Python xlrd module

人走茶凉 提交于 2020-02-23 12:46:43

问题


According to the documentation of the XLRD module,

 row_values(rowx, start_colx=0, end_colx=None)

"Returns a slice of the values of the cells in the given row."

And given the following python code:

import xlrd
wb=xlrd.open_workbook("xl1.xlsx")
sh = wb.sheet_by_index(0)
for rownum in range(sh.nrows):
    print sh.row_values(rownum) 

The output is:

 [12.0, u'test.0']
 [34.0, u'te.st']
 [u'test123', u'12.test']

and the exel file holds the following data:

12.0 , test.0 , 34.0 , te.st , test123 , 12.test

So, what type of data structure do I get as a line according to the output? Its not a tuple, (becasue when printing a tuple type, there is no u' as a prefix to a string), and what is the meaning of the u' ? And also its like we have two types of data in the data structure - int and "non int". Is it true? I could not find any information about this in the documentation. Thanks!


回答1:


row_values returns a list of cell values with appropriate data types. Data type of each item in the list depends on the cell type in the source excel file.

There could be items with only string, float and int data types, see this mapping table (find there docs for Cell) for more info.

u just means that this is a unicode string. Documentation explains it pretty well.

Hope that helps.




回答2:


First things first!
I don't know which version of xlrd you are using but according to the latest version(xlrd 0.9.4 at the time of writing this answer):

row_values() 

will return you with the values in the row in the form of a list. This can be verified by typing:

>>> type(sh.row_values(2))
<type 'list'>

Next to get the values of the row including the data types, you can type the following:

>>> sh.row(2)

Which will return you something like this:

[number:12.0, text:u'test.0']

Meaning that the first item in the list is a number and the second item is a string or also called as text.

Finally the u in front of the string means that it is Unicode encoding. Remember that xlrd module will try to render each and every data in excel sheet to unicode encoding and so you are seeing u

xlrd module official documentation



来源:https://stackoverflow.com/questions/17467542/python-xlrd-module

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!