gpt4 book ai didi

python - csv DictReader 上的 "Doing work"失败

转载 作者:行者123 更新时间:2023-11-30 23:02:38 26 4
gpt4 key购买 nike

我正在编写一个脚本,需要将 CSV 读入 DictReader,在字段上做一些工作(数据修改),然后将 DictReader 输出到通过 DictWriter 读取 csv。

如果我读取 CSV 然后写入 Dict,则该过程有效。

#Create the sample file
headers = ['Symbol', 'Price', 'Date', 'Time', 'Change', 'Volume']
rows = [{'Symbol':'AA', 'Price':39.48, 'Date':'6/11/2007',
'Time':'9:36am', 'Change':-0.18, 'Volume':181800},
{'Symbol':'AIG', 'Price': 71.38, 'Date':'6/11/2007',
'Time':'9:36am', 'Change':-0.15, 'Volume': 195500},
{'Symbol':'AXP', 'Price': 62.58, 'Date':'6/11/2007',
'Time':'9:36am', 'Change':-0.46, 'Volume': 935000},
]

#Open sample file
with open('stocks.csv','w') as f:
f_csv = csv.DictWriter(f, headers)
f_csv.writeheader()
f_csv.writerows(rows)

#Output the dict
with open('stocks.csv', 'r') as file:
csvread = csv.DictReader(file, delimiter=',')

with open('out.csv', 'w') as out:
headertowrite = ['Time', 'Symbol', 'NewColumn']
writer = csv.DictWriter(out, headertowrite, extrasaction='ignore')
writer.writeheader()
writer.writerows(csvread)
#Works!

但是 - 如果我添加新列,似乎我会丢失 DictReader 中的所有数据:

headers = ['Symbol', 'Price', 'Date', 'Time', 'Change', 'Volume']
rows = [{'Symbol':'AA', 'Price':39.48, 'Date':'6/11/2007',
'Time':'9:36am', 'Change':-0.18, 'Volume':181800},
{'Symbol':'AIG', 'Price': 71.38, 'Date':'6/11/2007',
'Time':'9:36am', 'Change':-0.15, 'Volume': 195500},
{'Symbol':'AXP', 'Price': 62.58, 'Date':'6/11/2007',
'Time':'9:36am', 'Change':-0.46, 'Volume': 935000},
]

with open('stocks.csv','w') as f:
f_csv = csv.DictWriter(f, headers)
f_csv.writeheader()
f_csv.writerows(rows)

with open('stocks.csv', 'r') as file:
csvread = csv.DictReader(file, delimiter=',')


for row in csvread:
row['NewColumn'] = '1'

with open('out.csv', 'w') as out:
headertowrite = ['Time', 'Symbol', 'NewColumn']
writer = csv.DictWriter(out, headertowrite, extrasaction='ignore')
writer.writeheader()
writer.writerows(csvread)
#Out.csv is blank!

有没有办法可以在编写 DictReader 之前对其进行操作?

最佳答案

由于 csvread 已被您的第一个 for 循环完全消耗,您可能会注意到,我们的 with 语句将输出一个空白文件。

您应该:

--> 将行保存到变量中

import csv
headers = ['Symbol', 'Price', 'Date', 'Time', 'Change', 'Volume']
rows = [{'Symbol':'AA', 'Price':39.48, 'Date':'6/11/2007',
'Time':'9:36am', 'Change':-0.18, 'Volume':181800},
{'Symbol':'AIG', 'Price': 71.38, 'Date':'6/11/2007',
'Time':'9:36am', 'Change':-0.15, 'Volume': 195500},
{'Symbol':'AXP', 'Price': 62.58, 'Date':'6/11/2007',
'Time':'9:36am', 'Change':-0.46, 'Volume': 935000}]

with open('stocks.csv','w') as f:
f_csv = csv.DictWriter(f, headers)
f_csv.writeheader()
f_csv.writerows(rows)

with open('stocks.csv', 'r') as file:
csvread = csv.DictReader(file, delimiter=',')

rows = []
for row in csvread:
row['NewColumn'] = '1'
rows.append(row)

with open('out.csv', 'w') as out:
headertowrite = ['Time', 'Symbol', 'NewColumn']
writer = csv.DictWriter(out, headertowrite, extrasaction='ignore')
writer.writeheader()
writer.writerows(rows)

--> 在将输出文件的 with 语句中进行修改(您读取一行,修改它并写入它。

import csv
headers = ['Symbol', 'Price', 'Date', 'Time', 'Change', 'Volume']
rows = [{'Symbol':'AA', 'Price':39.48, 'Date':'6/11/2007',
'Time':'9:36am', 'Change':-0.18, 'Volume':181800},
{'Symbol':'AIG', 'Price': 71.38, 'Date':'6/11/2007',
'Time':'9:36am', 'Change':-0.15, 'Volume': 195500},
{'Symbol':'AXP', 'Price': 62.58, 'Date':'6/11/2007',
'Time':'9:36am', 'Change':-0.46, 'Volume': 935000}]

with open('stocks.csv','w') as f:
f_csv = csv.DictWriter(f, headers)
f_csv.writeheader()
f_csv.writerows(rows)

with open('stocks.csv', 'r') as file:
csvread = csv.DictReader(file, delimiter=',')

with open('out.csv', 'w') as out:
headertowrite = ['Time', 'Symbol', 'NewColumn']
writer = csv.DictWriter(out, headertowrite, extrasaction='ignore')
writer.writeheader()
for row in csvread:
row['NewColumn'] = '1'
writer.writerow(row)

保重!第二个解决方案中是 writerow 而不是 writerows!

请注意,我还强烈推荐第二种解决方案,它在内存消耗方面更具可扩展性。

关于python - csv DictReader 上的 "Doing work"失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34404176/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com