how to write unicode to a txt file?

Discussion in 'Python' started by Frank Potter, Jan 17, 2007.

  1. Frank Potter

    Frank Potter Guest

    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?
    Thanks!
     
    Frank Potter, Jan 17, 2007
    #1
    1. Advertisements

  2. Frank Potter

    Peter Otten Guest

    You have to tell Python what encoding to use (i. e how to translate the
    codepoints into bytes):
    u'\xe4hnlicher als gew\xf6hnlich \xfcblich'

    You would perhaps use 'gb18030' instead of 'utf8'.

    Peter
     
    Peter Otten, Jan 17, 2007
    #2
    1. Advertisements

  3. You need to use the method open from module codecs:

    So, then, from command line:

    [email protected]:~$ file pru_uni.txt
    pru_uni.txt: UTF-8 Unicode text

    :)

    Regards,
     
    Facundo Batista, Jan 17, 2007
    #3
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.