2013-03-11 119 views
0

我有一個excel文件(xlsx)。這些值被讀作unicode值。從excel讀取unicode值到字符串

wb = xlrd.open_workbook('file.xlsx') 
sh = wb.sheet_by_index(0) 
first_column = sh.col_values(0) 
snd_column = sh.col_values(1) 

輸出爲以下形式:

first_column=['', u'here', u'here i am', u'where', u'where i am'] 
snd_column=['', u'20 km', ' ', u'10 km', u'23 km'] 

空單元被讀取爲正常空字符串。

如何以字符串形式直接輸出/讀取文件。像

first_coulmn=['', 'here', 'here i am', 'where', 'where i am'] 
snd_coulmn=['', '20 km', ' ', '10 km', '23 km'] 

我在尋找的是計算有效的方法。有小費嗎?

+3

爲什麼你需要*字節字符串? – 2013-03-11 20:29:56

+0

@Martijn Pieters在使用unicode值的字典上進行操作時,它變得非常麻煩。 – Zero 2013-03-11 20:42:20

+3

在Python 2中,如果您只有ASCII數據,則可以自由比較字節字符串和unicode值。以字典鍵的形式存儲Unicode,然後用字節字符串查看值就行了。 – 2013-03-11 20:44:56

回答

0

如何:

first_column = [str(v) for v in first_column] 
+0

如果列數很多,那會使代碼變慢。我在想可能有一些方法來調用'xlrd.open_workbook('file.xlsx')'?直接將unicode值讀入字符串形式。 – Zero 2013-03-11 20:39:23

0

可以使用STR()函數從Unicode轉換爲字符串。你在問什麼?

-1
  • worksheet.cell_value(row_index,coluna_sample)給我 - > u'7690088954'
  • str(worksheet.cell(row_index,coluna_sample).value)給我 - > '7690088954'

正如所建議的aestrivex