how to write unicode to a txt file?

Frank Potter could.net at gmail.com
Wed Jan 17 17:28:14 CET 2007


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!




More information about the Python-list mailing list