data extraction from xls using xlrd in python

最后都变了- 提交于 2019-12-13 00:40:58

问题


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)

回答1:


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



回答2:


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 ]



回答3:


I solved it by doing

str(variable_name)



回答4:


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.



来源:https://stackoverflow.com/questions/26672349/data-extraction-from-xls-using-xlrd-in-python

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