I have this code:
myrows=[]
for r in range(riga+1,nrows):
myrow=[]
for c in range(ncols):
cell=mysheet.cell_value(r,c) ##reading some text, cell is
unicode object
myrow.append(cell)
myrows.append(myrow)
idx=range(len(colnames)) ##colnames=['label1','label2','label3']
colnames=[item.replace('.','_') for item in colnames]
records=[]
for row in myrows: records.append(TAG['item'](*[TAG[colnames[i]]
(row[i]) for i in idx]))
response.headers['Content-Type']='application/xml'
return str(TAG['root'](*records))
As commented above cell is unicode object so myrows is a list of lists
of unicode objects.
If cell is an ascii char everything is ok; if it is not I get:
Traceback (most recent call last):
File "C:\Python26\web2py\gluon\restricted.py", line 188, in
restricted
exec ccode in environment
File "c:/Python26/web2py/applications/xcel2xml/controllers/
default.py", line 334, in <module>
File "C:\Python26\web2py\gluon\globals.py", line 95, in <lambda>
self._caller = lambda f: f()
File "c:/Python26/web2py/applications/xcel2xml/controllers/
default.py", line 255, in step44
return gen_xml(nomefile,riga,images)
File "c:/Python26/web2py/applications/xcel2xml/controllers/
default.py", line 296, in gen_xml
return str(TAG['root'](*records))
File "C:\Python26\web2py\gluon\html.py", line 797, in __str__
return self.xml()
File "C:\Python26\web2py\gluon\html.py", line 780, in xml
(fa, co) = self._xml()
File "C:\Python26\web2py\gluon\html.py", line 771, in _xml
self.components])
File "C:\Python26\web2py\gluon\html.py", line 110, in xmlescape
return data.xml()
File "C:\Python26\web2py\gluon\html.py", line 780, in xml
(fa, co) = self._xml()
File "C:\Python26\web2py\gluon\html.py", line 771, in _xml
self.components])
File "C:\Python26\web2py\gluon\html.py", line 110, in xmlescape
return data.xml()
File "C:\Python26\web2py\gluon\html.py", line 790, in xml
return '<%s%s>%s</%s>' % (self.tag, fa, co, self.tag)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position
5: ordinal not in range(128)
>From Inspect Attributes:
args= ('ascii', 'label\xc3\xa0', 5, 6, 'ordinal not in range(128)')
And from Variables:
Variables
fa ''
self <gluon.html.__tag__ object at 0x033D2490>
self.tag u'label'
co 'label\xc3\xa0'
args ('ascii', 'label\xc3\xa0', 5, 6, 'ordinal not in range(128)')
the string raising the error is "labelĂ " and I see from Variables that
it was encoded in utf-8 by xml I think.
The original string was "label\xe0" (as I said a unicode string).
Any suggestion? This is driving me crazy..thank you
carlo