我有两个问题:
a)Python csv模块无法正确处理特定的csv文件
我在database.csv
"AAAAAAAA" , 5.4817, 0.0000, 0.0000, 0.0000, 65.8370
"n,m-BBBBBBBBB" , 1.7897, 675.3787, 1234.7865, 47.0000, 42.0070
请注意双引号和逗号之间有空格。
我使用以下脚本来读取文件,第一列将被解码为"AAAAAAAA "
,而不是"AAAAAAAA"
import csv
def read_csv_data(path):
with open(path, 'rU') as f:
f_csv = csv.reader(f)
for row in f_csv:
yield row
for row in read_csv_data('database.csv'):
print(row)
输出
['AAAAAAAA ', ' 5.4817', ' 0.0000', ' 0.0000', ' 0.0000', ' 65.8370']
['n,m-BBBBBBBBB ', ' 1.7897', ' 675.3787', ' 1234.7865', ' 47.0000', ' 42.0070']
要删除空格,我会这样做
import csv
def read_csv_data(path):
col_type = [str, float, float, float, float, float]
with open(path, 'rU') as f:
f_csv = csv.reader(f)
for row in f_csv:
row = tuple(cast(val.strip()) for cast, val in zip(col_type, row))
yield row
for row in read_csv_data('database.csv'):
print(row)
现在输出
('AAAAAAAA', 5.4817, 0.0, 0.0, 0.0, 65.8370)
('n,m-BBBBBBBBB', 1.7897, 675.3787, 1234.7865, 47.0, 42.007)
b)使用namedtuple将csv数据读入内存
使用相同的csv文件database.csv
,我使用另一个脚本:
import csv
from collections import namedtuple
def read_csv_data(path):
col_type = [str, float, float, float, float, float]
Gas = namedtuple("Gas", ["gas", "sf", "h1", "h2", "h3", "m"])
with open(path, 'rU') as f:
f_csv = csv.reader(f)
for row in f_csv:
row = list(cast(val.strip()) for cast, val in zip(col_type, row))
for row2 in map(Gas._make, row):
yield row2
for row in read_csv_data('database.csv'):
print(row)
错误是
Traceback (most recent call last):
File "read_dict.py", line 17, in <module>
for row in read_csv_data('database.csv'):
File "read_dict.py", line 13, in read_csv_data
for row2 in map(Gas._make, row):
File "<string>", line 21, in _make
TypeError: Expected 6 arguments, got 8
答案 0 :(得分:3)
而不是
for row2 in map(Gas._make, row):
yield row2
你只想要
yield Gas._make(row)
现在,您正在循环row
中的每个元素并在其上调用Gas._make
。这就是为什么你得到“预期的6个论点,得到8个” - 你试图用Gas
制作"AAAAAAAA"
个实例。
更改后,输出
Gas(gas='AAAAAAAA', sf=5.4817, h1=0.0, h2=0.0, h3=0.0, m=65.837)
Gas(gas='n,m-BBBBBBBBB', sf=1.7897, h1=675.3787, h2=1234.7865, h3=47.0, m=42.007)
PS:对于Python 3,您应该使用csv
打开文件以与newline=""
一起使用,请参阅here。