gpt4 book ai didi

python - 如何在csv库中使用Python的DictReader类?

转载 作者:行者123 更新时间:2023-11-28 22:04:12 26 4
gpt4 key购买 nike

我开始着手处理我的第一个 Python 项目,但在尝试使用 Python 的 csv.DictReader 类来使用 CSV 文件时遇到了困难。

代码如下:

import os
import csv

f = open('MyCsvFile.txt', 'rb')
d = csv.Sniffer().sniff(f.read(1024), ',')
csvDict = csv.DictReader(csvfile = f, dialect = d)

for line in csvDict:
print line['Balance Date ']

csv 文件大致如下所示:

"Balance Date ","Currency Code ","Main Account","Balance Float  - 1 Day",... forty more fields
"09/01/2011","EUR","4000519"," .00",...
"09/01/2011","GBP","4000519"," .00",...
"09/01/2011","JPY","4000519"," .00",...

Python 不喜欢我使用 DictReader

g:\work\csvtest>python csvtest.py
Traceback (most recent call last):
File "csvtest.py", line 6, in <module>
csvDict = csv.DictReader(csvfile = f, dialect = d)
TypeError: __init__() takes at least 2 arguments (2 given)

有人能给我指出正确的方向吗?

最佳答案

您需要将 csvfile 作为位置参数而不是关键字参数传递:

csvDict = csv.DictReader(f, dialect = d)

关于python - 如何在csv库中使用Python的DictReader类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7587105/

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