473,382 Members | 1,657 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Q: Cteni unicode retezcu ze souboru UTF-8 s BOM?

Ahoj všeci,

Tak nějak prakticky poprvé se dostávám k tomu,
jak přečíst unicode řetězce ze souboru, který
je uložen ve formátu UTF-8 se signaturou
na začátku (BOM). Nějak se mi nedaří.

Mám takovýto soubor.txt v UTF-8 s BOM
=====================================
První řádek.
Druhý řádek.
Třetí řádek.
Příšerně žluťoučký kůň úpěl ďábelské ódy.
=====================================

.... a pustím skript
=====================================
import codecs
f = codecs.open('soubor.txt', 'r', 'utf-8')
for line in f:
print repr(line)
print line[1:]
f.close()
=====================================

Výsledek vypadá takto
=====================================
C:\tmp>python a.py
u'\ufeffPrvn\xed \u0159\xe1dek.\r\n'
První řádek.

u'Druh\xfd \u0159\xe1dek.\r\n'
ruhý řádek.

u'T\u0159et\xed \u0159\xe1dek.\r\n'
řetí řádek.

u'P\u0159\xed\u0161ern\u011b \u017elu\u0165ou\u010dk\xfd k\u016f\u0148 \xfap\u011bl \u010f\xe1belsk\xe9 \xf3dy.\r\n'
říšerně žluťoučký kůň úpěl ďábelské ódy.
=====================================

Všimněte si, že na prvním řádku je \ufeff, což je Byte Order Mark,
který se tam vůbec nemá objevit. Jeví se mi to jako chyba.
Na všech řádcích záměrně nevypisuji printem první znak,
protože u toho prvního řádku to krachne (což je pochopitelné).
Řešil někdo něco podobného? Musí se BOM ukousávat ve vlastní režii?

Díky,
pepr
Mar 14 '07 #1
0 1242

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Honza Novacek | last post by:
Jak se daji nastavit prvotni prava souboru, ktery je vytvoren pomoci PHP skriptu? Diky, Honza Nováček
3
by: Michael Weir | last post by:
I'm sure this is a very simple thing to do, once you know how to do it, but I am having no fun at all trying to write utf-8 strings to a unicode file. Does anyone have a couple of lines of code...
48
by: Zenobia | last post by:
Recently I was editing a document in GoLive 6. I like GoLive because it has some nice features such as: * rewrite source code * check syntax * global search & replace (through several files at...
4
by: webdev | last post by:
lo all, some of the questions i'll ask below have most certainly been discussed already, i just hope someone's kind enough to answer them again to help me out.. so i started a python 2.3...
15
by: John Salerno | last post by:
Forgive my newbieness, but I don't quite understand why Unicode is still something that needs special treatment in Python (and perhaps elsewhere). I'm reading Dive Into Python right now, and it...
6
by: archana | last post by:
Hi all, can someone tell me difference between unicode and utf 8 or utf 18 and which one is supporting more character set. whic i should use to support character ucs-2. I want to use ucs-2...
1
by: willie | last post by:
>willie wrote: wrote:
18
by: Chameleon | last post by:
I am trying to #define this: #ifdef UNICODE_STRINGS #define UC16 L typedef wstring String; #else #define UC16 typedef string String; #endif ....
23
by: Allan Ebdrup | last post by:
I hava an ajax web application where i hvae problems with UTF-8 encoding oc chineese chars. My Ajax webapplication runs in a HTML page that is UTF-8 Encoded. I copy and paste some chineese chars...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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 using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.