Python: encode / decode problems

I was trying to insert values into  a table from a blog post.
Problem code:
filehandle.write(“, 1, ‘2009-06-03 17:11:58’, ‘2009-06-03 17:11:8’,’”)
e = d.entries[val]
content = e.content[0].value
#print content
filehandle.write(str(content))
Issue: Since we are trying to write a junk value in to a file, and this is not ascii supported we get the below error.
Error shown:
Traceback (most recent call last):
File “C:Python25programsfeedreader.py”, line 24, in <module>
filehandle.write(str(content))
UnicodeEncodeError: ‘ascii’ codec can’t encode characters in position 239-240: ordinal not in range(128)
Solution:
filehandle.write(“, 1, ‘2009-06-03 17:11:58’, ‘2009-06-03 17:11:8’,’”)
e = d.entries[val]
content = e.content[0].value
encoding = “ascii”
out = content.encode(encoding, “ignore”)
#print content
filehandle.write(str(content))
Type in the enoding and set it. you can ignore the value. In this way we can write it to a file

2 responses

  1. Hi, very nice post. I have been wonder’n bout this issue,so thanks for posting

    1. sure..good to hear it helped

Leave a Reply

Your email address will not be published. Required fields are marked *