带有随机双引号的CSV文件

2024-10-03 17:19:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个CSV文件,在某些字段中有双引号字符。当使用Python进行解析时,它开始忽略这些引号之间的分隔符。例如:

5695|258|03/21/2012| 15:16:02.000|info|Microsoft-Windows-Defrag|shrink estimation, (C:)|36|"6ybSr: c{q6: |Application|WKS-WIN732test.test.local|http://schemas.microsoft.com/win/2004/08/events/event|0x0080000000000000|0|0||0|0|C:\Users\test\EventLog\win7-32-test-c-drive\Application.evtx
5770|258|03/24/2012| 04:21:02.000|info|Microsoft-Windows-Defrag|boot optimization, (C:)|36|00 00 00 00 d3 03 00 00 ae 03 00 00 00 00 00 00 22 b6 30 df 64 79 c7 f6 e2 6c 1c 00 00 00 00 00 00 00 00 00|Application|WKS-WIN732test.test.local|http://schemas.microsoft.com/win/2004/08/events/event|0x0080000000000000|0|0||0|0|C:\Users\test\EventLog\win7-32-test-c-drive\Application.evtx
5843|258|03/27/2012| 07:38:36.000|info|Microsoft-Windows-Defrag|boot optimization, (C:)|36|jbg54t5t"gfb:*&hgfh|Application|WKS-WIN732test.test.local|http://schemas.microsoft.com/win/2004/08/events/event|0x0080000000000000|0|0||0|0|C:\Users\test\EventLog\win7-32-test-c-drive\Application.evtx

因此,它将两个双引号之间的所有内容作为单个字段读取:

^{pr2}$

(请参阅上面示例中的插入符号(^)。在

我怎么能忽略它?在

注意:我不想将整个文件读入RAM并替换字符。解决方案必须在遍历读取器中的行时有效。

分隔符是管道。我使用标准CSV技术阅读,并用已知编码解码:

import csv
known_encoding = 'utf-8'  # for mwe, real code fetches for each file

with open(self.current_file.file_path, 'rb') as f:
    reader = csv.reader(f, delimiter='|')
    for row in reader:
        row = [s.decode(known_encoding) for s in row]
        # do stuff with data in row

Tags: testinfocomhttpforapplicationwindowslocal
2条回答

我猜您的CSV文件从不包含带引号的字段,因此您可以使用quoting参数将其关闭:

csv.reader(f, delimiter='|', quoting=csv.QUOTE_NONE)

您可以将quoting设置为csv.QUOTE_NONE,如下所示:

import csv

with open('my_file', 'r') as f:
    csvreader = csv.reader(f, delimiter='|', quoting=csv.QUOTE_NONE)
    ....

相关问题 更多 >