2013-03-11 120 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