3 回答

TA贡献1789条经验 获得超10个赞
三重引号可能由csv
模块添加以转义现有引号。
所以而不是像这样的东西:
csvwriter.writeline(food, vote)
尝试类似:
csvwriter.writeline(food.strip('"'), vote)

TA贡献1806条经验 获得超8个赞
您可以使用csv.DictReader以便按名称collections.Counter对列进行寻址,使用 a来计算每种食物出现的次数,然后使用相应csv.writer地输出它们,例如:
import csv
from collections import Counter
with open('input_file') as fin, open('output_file', 'wb') as fout:
# Count occurrences of each FOODS type
votes = Counter(row['FOODS'] for row in csv.DictReader(fin, delimiter=';'))
# Create a csv.writer around the output file and write the header columns
csvout = csv.writer(fout, delimiter=';')
csvout.writerow(['FAVORITE_FOOD', 'VOTES'])
# Write the name and vote counts to the file
csvout.writerows(votes.items())
添加回答
举报