对于作业,我需要从 CSV 文件中按日期顺序生成投注列表。文件如下(样本):
Aintree, Red Rum,2017,5,12,11.58, won
Aintree, Hurricane Fly,2017,5,12,11.58, won
Aintree, Murder,2017,5,12,5, lost
Ayr, Corbiere,2016,11,3,25, lost
我想为在 CSV 中[2],[3],[4]以格式组合的每一行创建一个新列'%d-%b-%y'。然后,我需要使用这个新列作为关键,以[5]按日期顺序生成投注列表。
我在下面有这段代码,我是 python 的新手,我没有取得太大的成功,也不知道哪里出了问题。运行时会产生此错误:
Traceback (most recent call last):
File "date_bet.py", line 25, in <module>
get_date()
File "date_bet.py", line 10, in get_date
data = list(csv.reader(csvFile))
io.UnsupportedOperation: not readable
代码:
import csv
from datetime import datetime
def get_date():
with open('results.csv', 'a') as csvFile:
writer = csv.writer(csvFile)
reader = csv.reader(csvFile)
all = []
data = list(csv.reader(csvFile))
row = next(csvFile)
row.append([7])
all.append(row)
for row in data:
row.append((data[4],data[3],data[2]), '%d-%b-%y')
all.append(row)
writer.writerows(all)
date = datetime.date(row[7], '%x')
print(row[5], key = date)
get_date()
智慧大石
慕哥6287543
相关分类