Stripping new lines from strings?

ajung at suxers.de ajung at suxers.de
Tue Aug 15 04:46:22 EDT 2000


CHRIS <unk at unk.ororr> wrote:
2qy
2qyWhat is the best (fastest) way to strip new line characters from a
2qystring in Python? In C I would do:
2qy
2qychar *p;
2qyp = strchr(str, '\n');
2qyif(p)
2qy  *p = '\0';
2qy
2qy/* repeat process for '\r' character */

Usually I use the following:

lines = open('myfile','r').readlines()
for l in lines:
	l = l[:-1]
   ...

Cheers,
Andreas




More information about the Python-list mailing list