我有一个csv文件,其中有两个字段,一个表示性别,另一个表示城市。
用户必须键入城市,文件中有100行重复的各种名称,我如何按城市过滤这些数据?
list
gender,City
M,Barcelona
M,London
M,Antwerpen
M,Paris
F,Calgary
F,Calgary
F,Albertville
F,Albertville
F,Lillehammer
F,Lillehammer
import csv
with open('dados_resposta.csv') as csvfile:
readCSV = csv.reader(csvfile, delimiter=',')
cidade = []
mulheres = []
for row in readCSV:
Sex = row[0]
City = row[1]
cidade.append(City)
mulheres.append(Sex)
entrada = input('country: ')
首先询问城市,然后使用if语句进行循环和过滤
import csv
entrada = input('city: ')
mulheres = []
with open('dados_resposta.csv') as csvfile:
readCSV = csv.reader(csvfile, delimiter=',')
for row in readCSV:
Sex = row[0]
if row[1] == entrada:
mulheres.append(Sex)
print('Genders for {} are {}'.format(entrada, mulheres))
或者,使用熊猫