Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > how to write unicode to a txt file?

Reply
Thread Tools

how to write unicode to a txt file?

 
 
Frank Potter
Guest
Posts: n/a
 
      01-17-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(12

How to save the unicode string to the file, please?
Thanks!

 
Reply With Quote
 
 
 
 
Peter Otten
Guest
Posts: n/a
 
      01-17-2007
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(12
>
> How to save the unicode string to the file, please?
> Thanks!


You have to tell Python what encoding to use (i. e how to translate the
codepoints into bytes):

>>> txt = u"ähnlicher als gewöhnlich üblich"
>>> import codecs
>>> codecs.open("tmp.txt", "w", "utf8").write(txt)
>>> codecs.open("tmp.txt", "r", "utf8").read()

u'\xe4hnlicher als gew\xf6hnlich \xfcblich'

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

Peter


 
Reply With Quote
 
 
 
 
Facundo Batista
Guest
Posts: n/a
 
      01-17-2007
Frank Potter wrote:

> But it seems that python can't directly write unicode to a file,


You need to use the method open from module codecs:

>>> import codecs
>>> a = codecs.open("pru_uni.txt", "w", "utf-8")
>>> txt = unicode("campeón\n", "utf-8")
>>> a.write(txt)
>>> a.close()
>>>


So, then, from command line:

facundo@expiron:~$ file pru_uni.txt
pru_uni.txt: UTF-8 Unicode text



Regards,

--
.. Facundo
..
Blog: http://www.taniquetil.com.ar/plog/
PyAr: http://www.python.org/ar/


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
counting how often the same word appears in a txt file...But my codeonly prints the last line entry in the txt file dgcosgrave@gmail.com Python 8 12-19-2012 06:29 PM
Diff. between FileWriter("f.txt") and OutputStreamWriter(new FileOutputStream("f.txt")) ? Jochen Brenzlinger Java 7 09-15-2011 01:23 AM
Re: how to write unicode to a txt file? Jean-Paul Calderone Python 0 01-17-2007 04:40 PM
Opening a txt file to view ( i.e. readme.txt) Sameen C++ 2 08-29-2005 03:14 PM



Advertisments