Problem reading file with umlauts
stefan_ml at behnel.de
Tue Jul 7 16:04:09 CEST 2009
Claus Hausberger wrote:
> 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:
> # -*- 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"
> print c
Note that printing non-ASCII characters may not always work, depending on
More information about the Python-list