pruppert
pruppert

Reputation: 121

How do I add lines to list as single string items?

I am trying to add lines of text to a list. I have the following code:

theText = 'foo \n bar'
for line in theText:
    theList.append(line)
print theList

This code prints:

['f', 'o', 'o', ' ', '\n', ' ', 'b', 'a', 'r']

when I was expecting something more like:

['foo ', '\n bar']

Is there some way to get the entire line to be a single string item in the list?

Upvotes: 1

Views: 120

Answers (5)

Padraic Cunningham
Padraic Cunningham

Reputation: 180391

If you want the lines and to keep the delimiters use splitlines with keepends=True, the correct result is ['foo \n', ' bar'] as the newline is on the line with foo not bar :

theText = 'foo \n bar'
print(theText.splitlines(True))
['foo \n', ' bar']

Upvotes: 3

A.J. Uppal
A.J. Uppal

Reputation: 19264

What you could do, using purely python and no libraries is:

theText = theText.split('\n')
['\n'+theText[i] if i%2==1 else theText[i] for i in range(len(theText))]

Which yields the desired output:

>>> theText = theText.split('\n')
>>> ['\n'+theText[i] if i%2==1 else theText[i] for i in range(len(theText))]
['foo ', '\n bar']
>>> 

Step by step:

>>> theText = 'foo \n bar'
>>> theText = theText.split('\n')
>>> theText
['foo ', ' bar']
>>> [theText[i] for i in range(len(theText)) if i%2==1]
[' bar']
>>> ['\n'+theText[i] for i in range(len(theText)) if i%2==1]
['\n bar']
>>> ['\n'+theText[i] if i%2==1 else theText[i] for i in range(len(theText))]
['foo ', '\n bar']
>>>   

Upvotes: 0

Adam Smith
Adam Smith

Reputation: 54173

If you require that output, try this:

result = [word if i==0 else "\n" + word for 
              i,word in enumerate(theText.splitlines())]

enumerate pairs up the results of its argument with numbers, e.g.:

enumerate("abcd") == [(0, 'a'), (1, 'b'), (2, 'c'), (3, 'd')] # roughly

So we're saying to split theText on newline, and give us the word itself if it's the first word in the sentence, otherwise add "\n" to the front of it.

Upvotes: 0

David Reeve
David Reeve

Reputation: 871

You need to split your string by newlines. Otherwise it reads through each character in the string. re.split will retain the delimiters if you hold it in a capture group:

import re
...
for line in re.split( '(\n)',theText ):
    theList.append(line)

However, this will put the newline in a separate item, like this:

['foo ','\n',' bar']

jaheba's answer suggests using re.findall, which would give you the output you expect:

for line in re.findall( '(\n?.+)',theText ):
    theList.append(line)

Or, if you want to exclude the delimiter, you can completely ignore regex and just use str.split:

for line in theText.split('\n'):
    theList.append(line)

Upvotes: 2

jaheba
jaheba

Reputation: 88

You can use regular expressions:

re.findall('(\n?.+)', theText)

Edit:

Just to clarify the behavior of the loop. If you iterate over a string (i.e. for char in string) you get the string character by character. This is consistent with the index of each char (char_0 = string[0], char_1 = sring[1], ...). In contrast file-objects behave different. An iteration over a file-descriptor yields the lines of the file. So if your text was a file instead which contains the text you would achieve the expected result.

Upvotes: 0

Related Questions