Re: how to write unicode to a txt file?

Discussion in 'Python' started by Jean-Paul Calderone, Jan 17, 2007.

  1. On 17 Jan 2007 08:28:14 -0800, Frank Potter <> wrote:
    >I want to change an srt file to unicode format so mpalyer can display
    >Chinese subtitles properly.
    >I did it like this:
    >
    >txt=open('dmd-guardian-cd1.srt').read()
    >txt=unicode(txt,'gb18030')
    >open('dmd-guardian-cd1.srt','w').write(txt)
    >
    >But it seems that python can't directly write unicode to a file,
    >I got and error at the 3rd line:
    >UnicodeEncodeError: 'ascii' codec can't encode characters in position
    >85-96: ordinal not in range(128)
    >
    >How to save the unicode string to the file, please?


    You cannot save unicode to a file. Files can only contain bytes. You
    can encode the unicode into a str and then write the str to the file.

    f = open('...', 'w')
    f.write(txt.encode(encoding))
    f.close()

    The encoding you select must be able to represent each code point in the
    unicode object you encode with it. The "ascii" encoding cannot encode all
    of your code points, so when Python implicitly uses it, you get the
    exception above. If you use an encoding like "utf-8" which can represent
    every code point, you won't see this exception. You could also use the same
    encoding which you used to decode the str originally, "gb18030", which will
    be able to represent every code point, since it produced them in the first
    place. Of course, if you do this, you may as well not even bother to do the
    decoding in the first place, unless you have some code in between the input
    and output steps which manipulates the unicode in some way.

    Jean-Paul
    Jean-Paul Calderone, Jan 17, 2007
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ml
    Replies:
    0
    Views:
    1,427
  2. Frank Potter

    how to write unicode to a txt file?

    Frank Potter, Jan 17, 2007, in forum: Python
    Replies:
    2
    Views:
    429
    Facundo Batista
    Jan 17, 2007
  3. Sameen
    Replies:
    2
    Views:
    415
    Victor Bazarov
    Aug 29, 2005
  4. Jochen Brenzlinger
    Replies:
    7
    Views:
    5,447
    Roedy Green
    Sep 15, 2011
  5. Ram
    Replies:
    3
    Views:
    236
    Tad McClellan
    Apr 26, 2007
Loading...

Share This Page