[prev in list] [next in list] [prev in thread] [next in thread] 

List:       python-list
Subject:    Re: Problem reading file with umlauts
From:       Stefan Behnel <stefan_ml () behnel ! de>
Date:       2009-07-07 14:04:09
Message-ID: 4a5355d9$0$31339$9b4e6d93 () newsspool4 ! arcor-online ! net
[Download RAW message or body]

Claus Hausberger wrote:
> Hello
> 
> I have a text file with is encoding in Latin1 (ISO-8859-1). I can't change that as \
> I do not create those files myself. 
> I have to read those files and convert the umlauts like ö to stuff like &oumol; as \
> the text files should become html files. 
> I have this code:
> 
> 
> #!/usr/bin/python
> # -*- coding: latin1 -*-
> 
> import codecs
> 
> f = codecs.open('abc.txt', encoding='latin1')
> 
> for line in f:
> print line
> for c in line: 
> if c == "ö":

You are reading Unicode strings, so you have to compare it to a unicode
string as in

	if c == u"ö":

> print "oe"
> else:
> print c

Note that printing non-ASCII characters may not always work, depending on
your terminal.

Stefan
-- 
http://mail.python.org/mailman/listinfo/python-list


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic