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! 2 11503
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?
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
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/ This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: burdeen |
last post by:
I can't find any way of writing a unicode, or UTF-8 format text file.
Right now i have a Unicode string that i write to the text file and
the unicode characters are replaced with ANSI question...
|
by: kent sin |
last post by:
Python support unicode, but some library don't. Write
is one of them.
When writing a csv file, The rows contains numbers and
unicode str. It is a little pain to first convert all
unicode str to...
|
by: Mark Miller |
last post by:
I have a char array and when I write it to a file using BinaryWriter the
position of the pointer is the size of the array + 1. For example: writing
char leaves the pointer at position 26 after...
|
by: Linda Chen |
last post by:
I need to write some unicode symbols such as degree symbol
(for example 36°) by using XMLTextWrite but couldn't make
it work. I found the degree char in unicode is '\u030A'
and here is my sample...
|
by: john |
last post by:
I need to produce a RTF-document which is filled with
data from a database.
I've created a RTF-document in WordPad (a template,
so to speak) which contains 'placeholders', for example
'<dd01>',...
|
by: Tibby |
last post by:
I need to read/write not only text files, but binary as well. It seems like on binary files, it doesn't right the last 10% of the file.
--
Thanks
---
Outgoing mail is certified Virus...
|
by: Billy Smith |
last post by:
I'm trying to write a little utility that will write some
binary data to a file via a javascript and Windows Script Host
under Windows XP. The only way to do this that I can find
is to convert...
|
by: raj.sinha |
last post by:
I have to "PUT" data to a Unicode file... a file that has the "FF FE"
mark at the beginning of the file.
How do i do that. What HTTP header do i need to send so that the data
is stored in the...
|
by: dmitrey |
last post by:
hi all,
what's the best way to write Python dictionary to a file?
(and then read)
There could be unicode field names and values encountered.
Thank you in advance, D.
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |