Python 3 - 读取带类型转换的 csv 文件

Python 3 - Read csv file with type conversion

我有两个问题:

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

而不是

for row2 in map(Gas._make, row):
    yield row2

你只是想要

yield Gas._make(row)

现在您正在遍历 row 中的每个元素并对其调用 Gas._make。这就是你得到 "Expected 6 arguments, got 8" 的原因——你试图从 "AAAAAAAA".

中创建一个 Gas 实例

改变这个后,输出是

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,您应该使用 newline="" 打开用于 csv 的文件,请参阅 here