Reading data in excel as normal string using xlrd python module

5,548 views
Skip to first unread message

anil kumar

unread,
Jul 4, 2011, 1:58:24 AM7/4/11
to python-excel
Hi,

I have an input excel file, which contains cells with some data like
"+CME ERROR: “SIM not inserted” or error 10. (Based on CMEE=2 or 1)".

When i try to read this module using xlrd module, the string is read
as unicode string and i get string with value
u'+CME ERROR: \u201cSIM not inserted\u201d or error 10. (Based on
CMEE=2 or 1)'

This is due to special charactors and other formula symbols like +, :
=.

How can i read exact string from the cell.

Please help us in this regard.

Thanks,
Anil kumar.

Shahid Islam

unread,
Jul 4, 2011, 3:12:51 AM7/4/11
to python...@googlegroups.com

To read data from specific cell try this one
 
def _read_xl(self):
    # open the work book            
        wb=xlrd.open_workbook('testfile.xls')

        #open the sheet you want to read its cells
        sht=wb.sheet_by_index(0)

        # this loop will print values for 2nd column and all the rows in the file.
        for r in range(sht.nrows):
            cell_col1=sht.cell(rowx=r,colx=1).value
            print cell_col1
        return {}

SHAHID ISLAM
OPEN ERP Developer 
Institute of Management Sciences, Peshawar

anil kumar

unread,
Jul 4, 2011, 3:20:24 AM7/4/11
to python...@googlegroups.com
Hi Shahid,

Thanks for the response.
I am able to read data from the sheet, but data is read as unicode, which is replacing some \u characters for + = , e.t.c

Example data in Cell:
CELL    :     "+CME ERROR: “SIM not inserted” or error 10. (Based on CMEE=2 or 1)".
After Read to variable : u'+CME ERROR: \u201cSIM not inserted\u201d or error 10. (Based on
CMEE=2 or 1)'


How do i get the exact string as it was in excel.

Thanks
Anil

--
You received this message because you are subscribed to the Google Groups "python-excel" group.
To post to this group, send an email to python...@googlegroups.com.
To unsubscribe from this group, send email to python-excel...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/python-excel?hl=en-GB.

John Machin

unread,
Jul 4, 2011, 8:41:25 AM7/4/11
to python...@googlegroups.com
On Monday, 4 July 2011 15:58:24 UTC+10, anil kumar wrote:

I have an input excel file, which contains cells with some data like
"+CME ERROR: “SIM not inserted” or error 10. (Based on CMEE=2 or 1)".

When i try to read this module using xlrd module, the string is read
as unicode string and i get string with value
u'+CME ERROR: \u201cSIM not inserted\u201d or error 10. (Based on
CMEE=2 or 1)'

This is due to special charactors and other formula symbols like +, :
=.

How can i read exact string from the cell.


Anil, Unicode text is expected. Please read the section on Unicode near the start of the xlrd documentation. The two "special" characters that you showed are just fancy quotes; such things are quite prevalent in text created with Microsoft Office software.

>>> import unicodedata
>>> for x in u'\u201c\u201d':
...     print repr(x), unicodedata.name(x)
...
u'\u201c' LEFT DOUBLE QUOTATION MARK
u'\u201d' RIGHT DOUBLE QUOTATION MARK

HTH,
John
Reply all
Reply to author
Forward
0 new messages