如何正确读取csv中格​​式错误的字符串?

时间:2019-06-15 08:35:10

标签: python string python-2.7 csv

在csv中,对于列,字符串中存在歧义。因此,我在列表中获得了6个值而不是5个值作为输出。

代码:

import csv
csv_data = csv.reader(file('test.csv'))
for row in csv_data:
    print row

我尝试将"替换为space,以获得不带引号的普通字符串,如下所示,

for row in csv_data:
    print [r.replace('"',' ') for r in row] # This did't worked as expected.

输入:

csv文件中的行如下所示,

1,2,"text1", "Sample text ""present" in csv, as this",5

"Sample text "present" in csv, as this" # Error due to this value.

输出:

['1', '2', 'text1', 'Sample text present" in csv', 'as this', 5]

预期输出:

['1', '2', 'text1', 'Sample text "present" in csv, as this', 5]

1 个答案:

答案 0 :(得分:1)

这几乎令人尴尬,但似乎至少可以在问题中显示的示例输入上起作用。它通过对csvreader读取的每一行进行后处理来工作,并尝试检测何时由于格式错误而错误地读取了它们,然后对其进行纠正。

import csv

def read_csv(filename):
    with open(filename, 'rb') as file:
        for row in csv.reader(file, skipinitialspace=True, quotechar=None):
            newrow = []
            use_a = True
            for a, b in zip(row, row[1:]):
                # Detect bad formatting.
                if (a.startswith('"') and not a.endswith('"')
                        and not b.startswith('"') and b.endswith('"')):
                    # Join misread field backs together.
                    newrow.append(', '.join((a,b)))
                    use_a = False
                else:
                    if use_a:
                        newrow.append(a)
                    else:
                        newrow.append(b)
                        use_a = True
            yield [field.replace('""', '"').strip('"') for field in newrow]

for row in read_csv('fmt_test2.csv'):
    print(row)

输出:

['1', '2', 'text1', 'Sample text "present" in csv, as this', '5']