Prathik Prathik - 7 months ago 18
Python Question

data extraction from xls using xlrd in python

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)

Answer

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
Comments