473,385 Members | 1,311 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Yet another UnicodeDecodeError problem

ashitpro
542 Expert 512MB
Expand|Select|Wrap|Line Numbers
  1. getResponse="HTTP/1.1 200 OK\r\nContent-Length: %d\r\nCache-Control: no-cache\r\n\r\n%s" % (payloadLength,byteString)
  2. getResponse = unicode(getResponse)
  3.  
At second line I get UnicodeDecodeError.
I am trying to replace "200 OK" from getResponse string with "500", using getResponse.replace(...)

Internally it convert string to unicode, I have just made it explicitly for better understanding.

Any help?
Sep 7 '10 #1
6 2159
bvdet
2,851 Expert Mod 2GB
What version of Python? Is your default encoding "ascii"? When converting from a standard string to a unicode string, a UnicodeError exception may be raised if a character that cannot be converted is encountered. A full traceback may provide the direct cause of the exception.
Expand|Select|Wrap|Line Numbers
  1. >>> s = "abcdef%s" % ("\xfc")
  2. >>> print s
  3. abcdefü
  4. >>> unicode(s)
  5. Traceback (most recent call last):
  6.   File "<interactive input>", line 1, in ?
  7. UnicodeDecodeError: 'ascii' codec can't decode byte 0xfc in position 6: ordinal not in range(128)
  8. >>> 
Sep 7 '10 #2
ashitpro
542 Expert 512MB
I am using python 2.6
Default encoding is "ascii"..

How do you tackle this problem....?

Let me make this more straight..

getResponse = getResponse + (some binary data)

getResponse.Replace("this_str","that_str")

Obviously, in second statement, it will try to decode to 'ascii' and throw exception.

Is there any standard way to deal with binary data in string, which could solve my problem?
Sep 7 '10 #3
bvdet
2,851 Expert Mod 2GB
Check out the struct module.
Sep 7 '10 #4
dwblas
626 Expert 512MB
Someday I will have to take the time to learn Unicode, or just switch to Python3.X. A workaround until then is to drop down to decimal values.
Expand|Select|Wrap|Line Numbers
  1. s="abcdef%s200 OK\r\notherstuff" % ("\xfc")
  2. to_find = [ord(ltr) for ltr in "200 OK"]
  3. new_str_list = []
  4. len_s = len(s)
  5. for ctr in range(len_s):
  6.     found = False
  7.     ltr=s[ctr]
  8.     if ord(ltr) == to_find[0]:     ## first characters match
  9.         ## assumes the range will not go past end of line
  10.         found = True
  11.         for x in range(len(to_find)):
  12.             if ord(s[ctr+x]) != to_find[x]:
  13.                 found = False         ## not a match
  14.     if found :
  15.         new_str_list.append("5")      ## replace "2"
  16.     else:        
  17.         new_str_list.append(ltr)
  18. print "".join(new_str_list) 
Sep 8 '10 #5
ashitpro
542 Expert 512MB
I don't have problem with switching to python 3.x

Will that make any difference? If yes, how?
Sep 9 '10 #6
dwblas
626 Expert 512MB
"In Python 3, all strings are sequences of Unicode characters."
See Section 4.3 at Dive Into Python 3 for more info.
Sep 9 '10 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Ruslan | last post by:
Hi, everybody. In this excerpt of code enc = 'some_type_of_encoding' def _encode(v): if isinstance(v, UnicodeType): v = v.encode(v) return v
2
by: dbri.tcc | last post by:
Hello I am getting somewhat random UnicodeDecodeError messages in my program. It is random in that I will be going through a pysqlite database of records, manipulate the results, and it will...
4
by: Robin Haswell | last post by:
Okay I'm getting really frustrated with Python's Unicode handling, I'm trying everything I can think of an I can't escape Unicode(En|De)codeError no matter what I try. Could someone explain to...
1
by: Karl | last post by:
error msg: Mod_python error: "PythonHandler mod_python.publisher" Traceback (most recent call last): File "/usr/lib/python2.3/site-packages/mod_python/apache.py", line 299, in HandlerDispatch...
4
by: Oleg Parashchenko | last post by:
Hello, I'm working on an unicode-aware application. I like to use "print" to debug programs, but in this case it was nightmare. The most popular result of "print" was: UnicodeDecodeError:...
0
by: John Machin | last post by:
On Apr 25, 9:15 pm, "andreas.prof...@googlemail.com" <andreas.prof...@googlemail.comwrote: Guessing is no substitute for reading the manual. print has nothing to do with your problem; the...
3
by: Jorgen Bodde | last post by:
Hi All, I am relatively new to python unicode pains and I would like to have some advice. I have this snippet of code: def playFile(cmd, args): argstr = list() for arg in...
0
by: Jorgen Bodde | last post by:
Hi Edwin, Filemask is obvious as it is assigned in the python code itself. It is "%file%". The idea is that the file clicked is substituted for the "%file%" by the replace action. The file that...
3
by: Gilles Ganault | last post by:
Hello I'm getting this error while downloading and parsing web pages: ===== title = m.group(1) UnicodeDecodeError: 'ascii' codec can't decode byte 0xe9 in position 48: ordinal not in...
1
by: aberry | last post by:
I am facing an error on Unicode decoding of path if it contain a folder/file name starting with character 'u' >>> fp = "C:\\ab\\anil" >>> unicode(fp, "unicode_escape") u'C:\x07b\x07nil' >>> fp...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.