我只是想将xlsx文件转换为csv文件,然后从该csv文件中读取数据 - 我正在使用xlsx2csv工具,这似乎是合法的,但我得到了这篇文章底部的错误,我坦率地不明白 - 是不是restkey和restval应该确保填充空键?我滥用xlsx2csv工具了吗?帮助将不胜感激 - 谢谢!
这是我的代码:
from xlsx2csv import *
import csv
file_name = 'C:\filepath\file.xlsx'
my_file = open(file_name)
dialect = csv.Sniffer().sniff(my_file.read(1024))
ptc_file.seek(0)
f = open(file_name[:-4]+'csv', "w")
xlsx2csv(file_name, f)
f.close()
dictreader = csv.DictReader(my_file, restkey="filler_key", restval="filler_field", dialect=dialect)
for row in dictreader:
print '%s' % row['Field']
当我运行此操作时,我收到以下错误:
Traceback (most recent call last):
File "C:\filepath\conversiontest.py", line 16, in <module>
for row in dictreader:
File "C:\Python27\lib\csv.py", line 103, in next
self.fieldnames
File "C:\Python27\lib\csv.py", line 90, in fieldnames
self._fieldnames = self.reader.next()
Error: line contains NULL byte