125

I have a wallop of a string with many lines. How do I read the lines one by one with a for clause? Here is what I am trying to do and I get an error on the textData var referenced in the for line in textData line.

for line in textData
    print line
    lineResult = libLAPFF.parseLine(line)

The textData variable does exist, I print it before going down, but I think that the pre-compiler is kicking up the error.

lfurini
  • 3,729
  • 4
  • 30
  • 48
DKean
  • 2,017
  • 6
  • 19
  • 28
  • You need to split your line into separate strings. Having a "/n" in it doesn't make it a separate string. – N_A Mar 14 '13 at 23:36
  • What error are you getting (with the traceback)? What's the value in `textData`? Can you give us a [SSCCE](http://sscce.org), something we can run and see the same problem as you so we can explain it to you? – abarnert Mar 15 '13 at 00:44
  • Also, what is the "pre-compiler" that you think is "kicking up the error"? – abarnert Mar 15 '13 at 00:45

3 Answers3

222

What about using .splitlines()?

for line in textData.splitlines():
    print(line)
    lineResult = libLAPFF.parseLine(line)
jfs
  • 399,953
  • 195
  • 994
  • 1,670
Benjamin Gruenbaum
  • 270,886
  • 87
  • 504
  • 504
  • @BenjaminGruenbaum: `"/r"` and `"/n"` are just normal two-character strings consisting of a slash and a letter. Did you mean `"\n"`? – abarnert Mar 15 '13 at 00:46
  • 1
    Is it a copy or an iterator? – user3226167 Sep 05 '17 at 12:22
  • @user3226167 it returns a _list_. – Benjamin Gruenbaum Sep 05 '17 at 17:12
  • Is there an iterator version of splitlines that would not create list, but instead return in iterator? – Youda008 Oct 01 '18 at 15:27
  • There is StringIO, but it is a lower level interface. That said in this case you already have a string - if you want to iterate an iterator (a file for example) line by line that's another question. Remember - in this case you already have a string. – Benjamin Gruenbaum Oct 01 '18 at 15:35
  • Yea, but constructing an array of them seems kinda wasteful when you just want to read one line at a time. Imagine it's some really big string, then constructing an array of lines more than duplicates the memory used. – Youda008 Oct 15 '18 at 10:35
  • Memory concerns and having a large string seem like two mutually exclusive patterns. However - I totally get there are some cases where one would want this - for that there is StringIO. – Benjamin Gruenbaum Oct 15 '18 at 12:21
6

by splitting with newlines.

for line in wallop_of_a_string_with_many_lines.split('\n'):
  #do_something..

if you iterate over a string, you are iterating char by char in that string, not by line.

>>>string = 'abc'
>>>for line in string:
    print line

a
b
c
thkang
  • 11,215
  • 14
  • 67
  • 83
  • 3
    For anyone reading this now, use str.splitlines() function instead. Some edge cases arise with this solution. For ex. say you iterate over each line, if there is a \n at the end of the final line of the object then your for loop will try and perform a task on an empty line causing errors. Discussed above in accepted solution. – thefrollickingnerd Jun 12 '20 at 02:39
4

This answer fails in a couple of edge cases (see comments). The accepted solution above will handle these. str.splitlines() is the way to go. I will leave this answer nevertheless as reference.

Old (incorrect) answer:

s =  \
"""line1
line2
line3
"""

lines = s.split('\n')
print(lines)
for line in lines:
    print(line)
P.R.
  • 3,785
  • 1
  • 27
  • 47
  • 2
    `split('\n')` will retain any `\r` characters within the sting. Carriage returns won't get deleted, which could be a problem. – Toothpick Anemone Oct 25 '19 at 10:00
  • very good point. I think this answer is not correct because there are a couple of edge cases that `splitlines()` will catch, but my solution does not. https://docs.python.org/3/library/stdtypes.html#str.splitlines – P.R. Oct 25 '19 at 10:46