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

Ivan Voras ivoras at __geri.cc.fer.hr
Fri Feb 13 16:10:18 EST 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?

file("file2","w").write("".join(
	[ch for ch in file("file1", "r").read()
	if ch in string.ascii_letters]))

but this will also strip line breaks and whatnot :)

(n.b. I didn't actualy test the above code, and wrote it because of 
amusement value :) )




More information about the Python-list mailing list