2014-12-24 76 views
-2

我有python腳本用於讀取Excel以XML格式,但它顯示出一些誤差AttributeError的:「浮動」對象有沒有屬性「編碼」 3

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") 
+0

感謝您寶貴的回答 –

相關問題