data extraction from xls using xlrd in python

2019-08-02 08:04发布

I am trying to extract the data from an .xls file and making a list but i am getting the list as [u'elem1', u'elem2', u'elem3'], but if i print separately i get as:

elem1
elem2
elem3

what is that u thing and how to remove it?

Here is my code...

from xlrd import open_workbook
xls=open_workbook('name.xls')
for sheets in xls.sheets():
    list1=[]
    for col in range(sheets.ncols):
        for rows in range(sheets.nrows):
            list1.append(sheets.cell(rows, col).value)
print(list1)
for i in list1:
    print(i)

4条回答
别忘想泡老子
2楼-- · 2019-08-02 08:47

For practical matters, the u in the beginning won't affect u. U can work with them as well unless you have some issues related to encoding it in different formats.

查看更多
萌系小妹纸
3楼-- · 2019-08-02 08:50

Assuming you are using Python 2.x, the u thing says that xlrd gives you unicode strings (what Excel strings really are). If you want to convert them in Python 2.7 strings, you have to encode them with the charset you use

Assuming you use latin1 (also knows as iso-8859-1 or (with minimal differences) windows-1252, you can transform your list of unicode strings in a list of latin1 strings that way :

strlist = [ elt.encode('latin1') for elt in list1 ]

or if you have only ASCII characters

strlist = [ str(elt) for elt in list1 ]
查看更多
一夜七次
4楼-- · 2019-08-02 08:55

I solved it by doing

str(variable_name)
查看更多
我只想做你的唯一
5楼-- · 2019-08-02 09:07

You can define the text as string,while appending data to the list in list1.append(str(sheets.cell(rows, col).value)) to remove [u' .The code will be:

   from xlrd import open_workbook
   xls=open_workbook('name.xls')
   for sheets in xls.sheets():
   list1=[]
   for col in range(sheets.ncols):
      for rows in range(sheets.nrows):
         list1.append(str(sheets.cell(rows, col).value))
   print(list1)
   for i in list1:
      print i
查看更多
登录 后发表回答