我有用于读取excel到xml格式的python脚本,但它显示了一些错误
val = sh.row_values(row)[i].encode("utf-8")
Attribute Error : 'float' object has no attribute 'encode'
这是我的代码:
import xlrd
wb = xlrd.open_workbook("my_excel_file.xlsx")
sh = wb.sheet_by_index(0)
tags = [n.replace(" ", "").lower() for n in sh.row_values(0)]
result = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<myItems>\n"
for row in range(1, sh.nrows):
result += " <item>\n"
for i in range(len(tags)):
tag = tags[i].encode("utf-8")
val = sh.row_values(row)[i].encode("utf-8")
result += " <%s>%s</%s>\n" % (tag, val, tag)
result += " </item>\n"
result += "</myItems>"
f = open("myfile.xml", "w")
f.write(result)
f.close()
答案 0 :(得分:4)
val = str(sh.row_values(row)[i]).encode("utf-8")