encoding confusions

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tim Arnold

    #1

    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(fna me,'w',encoding ='latin-1', errors='ignore' )
    tFile.write(sel f.contents)
    tFile.close()

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

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

    this is python2.4.1 (hpux)
    sys.getdefaulte ncoding()
    'ascii'

    thanks,
    --Tim Arnold


  • Marc 'BlackJack' Rintsch

    #2
    Re: encoding confusions

    In <eugrkh$7n7$1@f oggy.unx.sas.co m>, 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

    Comment

    Working...