I have a list that consists of 149 text strings. From each string, I would like to extract the 2nd and 7th lines of text. I'd like the results of each line to be put in a separate list.
I've tried (just for one line):
text = []
for string in list:
x = string.splitlines()[2]
text.append(x)
But I get the message "IndexError: list index out of range". I thought maybe I needed to do:
text = []
for string in list:
x = [string].splitlines()[2]
text.append(x)
But that gave me the message "AttributeError: 'list' object has no attribute 'splitlines'"
I'm using Python 3.6. Any ideas? Thanks for your help!
string.splitlineswas correct, you just don't have 3 lines in that specific entry, apparently. (Without knowing what your data looks like, I can't say much more)print(len(string.splitlines()))right before assigning x in your first attempt?string.splitlines()[1]- as 0 is the first element, 1 is the second and so on.