1

我只是想将 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
4

0 回答 0