我使用以下代码将一堆.xls文件分解为.csv文件。
import sys
import os
import xlrd
def explode(inputFile):
try:
os.mkdir(inputFile+".folder")
except OSError:
chill = None
with xlrd.open_workbook(inputFile) as wb:
for k in range(0,wb.nsheets):
sh = wb.sheet_by_index(k) # or wb.sheet_by_name('name_of_the_sheet_here')
with open(inputFile+".folder"+"/sheet"+str(k)+".csv", 'w') as f:
ret = ""
for r in range(sh.nrows):
for col in sh.row_values(r):
try:
ret+=str(col)+","
except UnicodeEncodeError:
ret+=","
ret = ret[:-1]
ret+="\n"
ret = ret[:-1]
f.write(ret)
dataDir = sys.argv[1]
os.chdir(dataDir)
Files = [name for name in os.listdir(".") if not os.path.isdir(name)]
for k in Files:
explode(k)
os.chdir("..")
这在我的机器上运行得很好(Ubuntu 16),但是当我的同事在Windows上运行代码时,会出现以下错误......
Traceback (most recent call last):
File "xlsToCsv.py", line 35, in <module>
explode(k)
File "xlsToCsv.py", line 25, in explode
f.write(ret)
File "C:\Users\AUser\Anaconda3\lib\encodings\cp1252.py", line 19, in encode
return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode character '\ufb01' in position 64253: character maps to <undefined>
我尝试将行# -*- coding: utf-8 -*-
添加到文件头部但没有效果。
我很乐意任何给我提供良好跨平台性能的建议,对此问题的任何见解都表示赞赏!
答案 0 :(得分:1)
第13行需要阅读
with open(inputFile+".folder"+"/sheet"+str(k)+".csv", 'w', encoding="utf-8") as f: