【发布时间】:2014-08-20 07:30:00
【问题描述】:
我已经提到了一些与 unicode 错误相关的帖子,但我的问题没有得到任何解决方案。我正在将 xlsx 转换为 csv 来自 6 张工作簿。 使用以下代码
def csv_from_excel(file_loc):
#file_acess check
print os.access(file_loc, os.R_OK)
wb = xlrd.open_workbook(file_loc)
print wb.nsheets
sheet_names = wb.sheet_names()
print sheet_names
counter = 0
while counter < wb.nsheets:
try:
sh = wb.sheet_by_name(sheet_names[counter])
file_name = str(sheet_names[counter]) + '.csv'
print file_name
fh = open(file_name, 'wb')
wr = csv.writer(fh, quoting=csv.QUOTE_ALL)
for rownum in xrange(sh.nrows):
wr.writerow(sh.row_values(rownum))
except Exception as e:
print str(e)
finally:
fh.close()
counter += 1
我在第 4 页出现错误
'ascii' codec can't encode character u'\u2018' in position 0: ordinal not in range(128)"
但位置 0 是空白的,并且它已转换为 csv 直到第 33 行。
我无法弄清楚。 CSV 是读取内容并放入我的数据结构的简单方法。
【问题讨论】:
标签: python csv unicode ascii codec