Prathik
Prathik

Reputation: 113

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)

Upvotes: 1

Views: 5165

Answers (4)

jack
jack

Reputation: 505

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

Upvotes: 3

codelearner336
codelearner336

Reputation: 61

I solved it by doing

str(variable_name)

Upvotes: 1

Mujtaba Hasan
Mujtaba Hasan

Reputation: 112

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.

Upvotes: 0

Serge Ballesta
Serge Ballesta

Reputation: 148880

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 ]

Upvotes: 2

Related Questions