Iterate over the lines of a string

前端 未结 6 1788
情深已故
情深已故 2020-11-28 03:15

I have a multi-line string defined like this:

foo = \"\"\"
this is 
a multi-line string.
\"\"\"

This string we used as test-input for a par

6条回答
  •  执念已碎
    2020-11-28 04:06

    You can iterate over "a file", which produces lines, including the trailing newline character. To make a "virtual file" out of a string, you can use StringIO:

    import io  # for Py2.7 that would be import cStringIO as io
    
    for line in io.StringIO(foo):
        print(repr(line))
    

提交回复
热议问题