way to remove all non-ascii characters from a file?

Gerhard Häring gh at ghaering.de
Tue Feb 17 19:04:55 CET 2004


omission9 wrote:
> I have a text file which contains the occasional non-ascii charcter.
> What is the best way to remove all of these in python?

Here's a simple example that does what you want:

 >>> orig = "Häring"
 >>> "".join([x for x in orig if ord(x) < 128])
'Hring'

-- Gerhard




More information about the Python-list mailing list