473,387 Members | 2,436 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,387 software developers and data experts.

encoding confusions

I have the contents of a file that contains French documentation.
I've iterated over it and now I want to write it out to a file.

I'm running into problems and I don't understand why--I don't get how the
encoding works.
My first attempt was just this:
< snipped code for classes, etc; fname is string, codecs module loaded.>
< self.contents is the French file's contents as a single string >

tFile = codecs.open(fname,'w',encoding='latin-1', errors='ignore')
tFile.write(self.contents)
tFile.close()

ok, so that didn't work and I read some more and did this:
tFile.write(self.contents.encode('latin-1'))

but that gives me the same error
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe9 in position 48:
ordinal not in range(128)

this is python2.4.1 (hpux)
sys.getdefaultencoding()
'ascii'

thanks,
--Tim Arnold
Mar 29 '07 #1
1 1081
In <eu**********@foggy.unx.sas.com>, Tim Arnold wrote:
I have the contents of a file that contains French documentation.
I've iterated over it and now I want to write it out to a file.

I'm running into problems and I don't understand why--I don't get how the
encoding works.
My first attempt was just this:
< snipped code for classes, etc; fname is string, codecs module loaded.>
< self.contents is the French file's contents as a single string >
What is the type of `self.contents`, `str` or `unicode`? You *decode*
strings to unicode objects and you *encode* unicode objects to strings.
It doesn't make sense to encode a string in 'latin-1' because it must be
decoded first and the "automatic" decoding assumes ASCII and barfs if
there's something non-ascii in the string.

Ciao,
Marc 'BlackJack' Rintsch

Mar 29 '07 #2

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

Similar topics

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...
23
by: Xah Lee | last post by:
The Concepts and Confusions of Pre-fix, In-fix, Post-fix and Fully Functional Notations Xah Lee, 2006-03-15 Let me summarize: The LISP notation, is a functional notation, and is not a...
30
by: Xah Lee | last post by:
The Concepts and Confusions of Prefix, Infix, Postfix and Fully Functional Notations Xah Lee, 2006-03-15 In LISP languages, they use a notation like “(+ 1 2)” to mean “1+2”....
0
by: sonuindia88 | last post by:
I am preparing c for entrance examinations..I am reading Ritchie and Deitel and practising questions from Yashwant Kanetkar...Here are some of the confusions i am having..your help is required.... ...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: 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...

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.