Python 打印具有相同列的行
我还是个新手,被困在这个案子里 我有一个csv文件,它与下面的类似Python 打印具有相同列的行,python,csv,dictionary,rows,Python,Csv,Dictionary,Rows,我还是个新手,被困在这个案子里 我有一个csv文件,它与下面的类似 import csv csvpath = "C:/Test/test.csv" with open(csvpath) as f: csv = csv.DictReader(f) for row in csv: print(row) 输出为: {'NAME': 'John', 'NICKNAME': 'Big John', 'COUNTRY': 'Canada', 'CIT
import csv
csvpath = "C:/Test/test.csv"
with open(csvpath) as f:
csv = csv.DictReader(f)
for row in csv:
print(row)
输出为:
{'NAME': 'John', 'NICKNAME': 'Big John', 'COUNTRY': 'Canada', 'CITY': 'Toronto'}
{'NAME': 'David', 'NICKNAME': 'Small Jogn', 'COUNTRY': 'Canada', 'CITY': 'Toronto'}
{'NAME': 'Alan', 'NICKNAME': 'The Bull', 'COUNTRY': 'England', 'CITY': 'London'}
{'NAME': 'Ethan', 'NICKNAME': 'The Hawk', 'COUNTRY': 'England', 'CITY': 'London'}
{'NAME': 'Ivan', 'NICKNAME': 'The Russian', 'COUNTRY': 'Russia', 'CITY': 'Moscow'}
{'NAME': 'Boris', 'NICKNAME': 'The Bear', 'COUNTRY': 'Russia', 'CITY': 'Moscow'}
是否可以只打印包含国家加拿大的行。然后我想要另一个循环来打印英国和俄罗斯的行。但是,该国家将一直被编辑,因此它将不相同,并且该列表中每天可能有不同的国家和国家数量。因此,基本上我需要以不同的for循环分别打印具有相同国家/地区的行。当它们都在一个数组中时
def print_countries(array,countryname):
print("results for: ", countryname)
for item in array:
if item['COUNTRY'] == countryname:
print(item)
使之成为一种功能。如果你想打印CSV文件中的每个国家,我会用每个名字构建一个数组
def get_countrynames(array):
countryname_list = []
for row in array:
if row['COUNTRY'] not in countryname_list:
countryname_list.append(row['COUNTRY'])
return countryname_list
with open(csvpath) as f:
csv = csv.DictReader(f)
names = get_countrynames(csv)
for country in names:
print_countries(csv,country)
希望这能起作用,不要测试我自己。下面的代码按国家对数据进行分组:(基于问题中的数据结构) 输出
Canada
{'NAME': 'John', 'NICKNAME': 'Big John', 'COUNTRY': 'Canada', 'CITY': 'Toronto'}
{'NAME': 'David', 'NICKNAME': 'Small Jogn', 'COUNTRY': 'Canada', 'CITY': 'Toronto'}
England
{'NAME': 'Alan', 'NICKNAME': 'The Bull', 'COUNTRY': 'England', 'CITY': 'London'}
{'NAME': 'Ethan', 'NICKNAME': 'The Hawk', 'COUNTRY': 'England', 'CITY': 'London'}
Russia
{'NAME': 'Ivan', 'NICKNAME': 'The Russian', 'COUNTRY': 'Russia', 'CITY': 'Moscow'}
{'NAME': 'Boris', 'NICKNAME': 'The Bear', 'COUNTRY': 'Russia', 'CITY': 'Moscow'}
根据某些条件从集合中选择项称为筛选 Python提供了内置函数来实现这一点,也可以使用或表达式来实现相同的结果 列表理解比
filter
快,而且被认为更“pythonic”,但我发现filter
有时使代码更容易理解。生成器表达式在语法上类似于列表理解,但使用的内存较少(并且只能循环一次)
下面的函数显示了如何使用这三个选项。它接受一个打开的文件和一个国家名称列表(作为字符串)作为参数,并打印出为每个国家找到的行
import csv
def print_rows(file, countries):
reader = csv.DictReader(file)
# Make a list of all the rows so we can loop over them more than once.
rows = list(reader)
for country in countries:
print('Printing rows for', country)
# Filter the rows by country.
# Using the filter built-in function.
filtered_rows = filter(lambda row: row['COUNTRY'] == country, rows)
# List comprehension.
# filtered_rows = [row for row in rows if row['COUNTRY'] == country]
# Generator expression.
# filtered_rows = (row for row in rows if row['COUNTRY'] == country)
for row in filtered_rows:
print(row)
print()
return
countries = ['Canada', 'Russia']
csvpath = "C:/Test/test.csv"
with open(csvpath) as f:
csv = csv.DictReader(f)
print_rows(f, countries)
输出:
为加拿大打印行
{'NAME':'John','昵称':'Big John','COUNTRY':'Canada','CITY':'多伦多'}
{'NAME':'David','昵称':'Small Jogn','国家':'加拿大','城市':'多伦多'}
俄罗斯的印刷行
{'NAME':'Ivan','昵称':'俄罗斯人','国家':'俄罗斯','城市':'莫斯科'}
{‘名字’:‘鲍里斯’,‘昵称’:‘熊’,‘国家’:‘俄罗斯’,‘城市’:‘莫斯科’}
这不是csv文件的外观。是的,它是csv,如果将与open(csvpath)一起使用f:csv=csv,它可以作为字典读取。DictReader(f)
您真的要在“不同循环”中打印,还是要按国家顺序打印文件中的行?@snakecharmerb是在不同循环中打印。第一个循环仅打印来自一个国家/地区的行。第二次循环从其他国家打印,以此类推。谢谢,但正如我所说,国家将在csv文件中每天更改,因此下次加拿大可能不会在csv文件中。谢谢,这是我问题的最接近答案,但是否可以不从字典中读取,而是从csv中读取,正如我在上面使用带open(csvpath)的作为f:csv=csv.DictReader(f`)
?@bbfl尝试读取csv并创建dict。这对您来说是一个很好的体验。
import csv
def print_rows(file, countries):
reader = csv.DictReader(file)
# Make a list of all the rows so we can loop over them more than once.
rows = list(reader)
for country in countries:
print('Printing rows for', country)
# Filter the rows by country.
# Using the filter built-in function.
filtered_rows = filter(lambda row: row['COUNTRY'] == country, rows)
# List comprehension.
# filtered_rows = [row for row in rows if row['COUNTRY'] == country]
# Generator expression.
# filtered_rows = (row for row in rows if row['COUNTRY'] == country)
for row in filtered_rows:
print(row)
print()
return
countries = ['Canada', 'Russia']
csvpath = "C:/Test/test.csv"
with open(csvpath) as f:
csv = csv.DictReader(f)
print_rows(f, countries)