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())
添加回答
舉報