我正在尝试读取包含 NUL 的 csv 文件带 CSV 阅读器我搜索了很多寻找一种没有错误地读取文件的方法,但找不到。
编辑:添加文件的相关部分:https://drive.google.com/open?id=1rtF3ck6QymtH4_n4iUjavPnxZiryq_Q4
我的代码:
with codecs.open(log_path, 'r') as csv_file:
log_reader = csv.DictReader(csv_file)
for line in log_reader:
if line['Addition Information'] == str
# do something
将不胜感激任何帮助谢谢阿维谢
csv.reader()
(因此也csv.DictReader()
(根本无法处理包含空字节的文件。
一种可能的解决方案是在读取输入文件时替换空字节,例如通过使用生成器表达式,因为reader()
将支持迭代器协议的任何对象作为参数:
with codecs.open(log_path, 'r') as csv_file:
log_reader = csv.DictReader((l.replace(' ', '') for l in csv_file))
for line in log_reader:
if line['Addition Information'] == str
# do something
尝试像这样修改代码:
with codecs.open(log_path, 'r', encoding='utf-8', errors='backslashreplace') as csv_file:
log_reader = csv.DictReader(csv_file)
for line in log_reader:
if line['Addition Information'] == str
# do something