[Tutor] Line continuation with readlines

bob bgailer at alum.rpi.edu
Wed Oct 5 16:56:52 CEST 2005


At 07:20 PM 10/3/2005, Craig MacFarlane wrote:
>Hello,
>
>Is there a way to make line continuation work with
>the readlines function?
>
>i.e.

Do you mean e.g.?

>   this is \
>     one line.

I assume the above is a 2 line file you wish to read using a file object's 
readlines method. There is nothing native to help you. I suggest you use 
the read method, then remove any sequence of \ followed by \n then split at \n.

input = file('c:/foo.txt').read()
input2 = input.replace('\\\n', '')
input3 = input2.split('\n')

Now you have a list of "logical" lines. Note there are no \n at the end. 
readlines leaves the \n at the end of the lines. 



More information about the Tutor mailing list