473,397 Members | 1,950 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,397 software developers and data experts.

Re: Encoding Text

Paul Jefferson wrote:
I'm learning this and I'm making a program which takes RSS feeds and
processes them and then outputs them to a HTML file.
The problem I have is that some of the RSS feeds contain chachters which
I think are outside of the ascii range as when I attempt to write the
file containing themI get the follwoign error:
File "C:\Users\Paul\Desktop\python\Feed reader\test.py", line 201, in
<module>
runner(1,"save.ahc")
File "C:\Users\Paul\Desktop\python\Feed reader\test.py", line 185, in
runner
h.write(html[countera])
UnicodeEncodeError: 'ascii' codec can't encode character u'\u2019' in
position 1147: ordinal not in range(128)
For the life of me I can't figure out what I should do to stop this - I
tried to change the html by doing a html[countera] =
unicode(html[countera]) but it didn't seem to chaneg anything.
I assume from the traceback that html[countera] contains a unicode
object and that h is a regular file open for writing. If that's the
case then you need to do one of two things to have the string written
correctly: either open/wrap the file via the codecs module to specify
the encoding through which unicode objects should be written; or
explicitly encode the unicode objects as they're written.

If you don't do one of those two things then Python will try to
convert your unicode object naively so that it can be written to
file, and it will use the ascii codec which can't handle anything
outside the basic stuff. You've got a right-quote mark in your
unicode, probably from a Windows program.

Since the latter is easier to demonstrate from the code above:

<code snippet>
h.write (html[countera].encode ("utf8"))
</code snippet>

but a more general-purpose solution is probably:

<code snippet>
import codecs

h = codecs.open ("save.ahc", "w", encoding="utf8")
..
..
h.write (html[countera])
</code snippet>

TJG
Jun 27 '08 #1
0 961

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Christos TZOTZIOY Georgiou | last post by:
This is a subject that comes up fairly often. Last night, I had the following idea, for which I would like feedback from you. This could be implemented as a function in codecs.py (let's call it...
4
by: Majed | last post by:
Hi , all I'm trying to write unicode to a file for another app (not developed with vs2003) to read it. I used StreamWriter with unicode encoding.but I was surprised that the streamwriter adds FFFE...
12
by: Tamir Khason | last post by:
I have Windows Form application recieved data from clipboard and convert its encoding based on some ruls. So doing following: //from source to multiple targets System.Text.Encoding targ1 =...
5
by: James Wong | last post by:
Dear all, I've a web service function and it contains a parameter in System.Text.Encoding. I found that the data type of this parameter in caller application becomes MyWebSvcName.Encoding...
0
by: Chris McDonough | last post by:
ElementTree's XML serialization routine implied by tree._write(file, node, encoding, namespaces looks like this (elided): def _write(self, file, node, encoding, namespaces): # write XML to file...
4
by: Bob | last post by:
Hi Need to produce a Doc with no encoding info. Is there anyway of doing this? Thanks Bob i.e. <?xml version=\"1.0\" ?>
4
by: Christina | last post by:
Hey Guys, Currently, I am using the below code: Dim oReqDoc as XmlDocument Dim requiredBytes As Byte() requiredBytes = System.Text.UTF8Encoding.UTF8.GetBytes(oReqDoc.InnerXml). Here, I am...
4
by: George | last post by:
Hi, I am puzzled by the following and seeking some assistance to help me understand what happened. I have very limited encoding knowledge. Our SAP system writes out a text file which includes...
0
by: Janusz Nykiel | last post by:
I've stumbled upon unexpected behavior of the .NET 2.0 System.Xml.XmlWriter class when using it to write data to a binary stream (System.IO.Stream). If the amount of data is less than a certain...
8
by: Erwin Moller | last post by:
Hi group, I could use a bit of guidance on the following matter. I am starting a new project now and must make some decisions regarding encoding. Environment: PHP4.3, Postgres7.4.3 I must...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.