PDA

View Full Version : Python: readlines issue




lynkynpark86
Dec 9, 2011, 07:50 AM
I'm using readlines, and if I do print 'Loading %s...' % (stringFromReadlines)
It prints this:
Loading TheTextLoadedFromFile
...

Notice the ... is on a new line. Why is this happening? It's like the string contains a line break.



subsonix
Dec 9, 2011, 08:08 AM
Why is this happening? It's like the string contains a line break.

That is probably the case then, a line is ended with a newline symbol. At least the readline() includes the newline.

lynkynpark86
Dec 9, 2011, 08:19 AM
That is probably the case then, a line is ended with a newline symbol. At least the readline() includes the newline.

So how do I avoid this? I need to have multiple lines in my file, and this only happens on the first line. How could I fix this?

subsonix
Dec 9, 2011, 08:27 AM
So how do I avoid this? I need to have multiple lines in my file, and this only happens on the first line. How could I fix this?

I don't know, strip the new line manually? Perhaps there is a more sensible way of solving it with Python, look at the docs.

lynkynpark86
Dec 9, 2011, 09:12 AM
I don't know, strip the new line manually? Perhaps there is a more sensible way of solving it with Python, look at the docs.

Figured it out! Not sure how clean it will be, but theString = theString.replace('\n','') seems to work fine. Thanks for the help!