Johnnerz
Johnnerz

Reputation: 1425

removing mysterious line break python

I have this code that runs through lines in text file like this:

09824747 18 n 02 archer 0 bowman 0 003 @ 09640897 n 0000 ~ 10290474 n 0000 ~i 10718145 n 0000 | a person who is expert in the use of a bow and arrow

L = line.split()
L2 = line.split('|')
synset_offset = L[0]
lex_filenum = L[1]
ss_type = L[2]
gloss = L2[1]

They way i print these out looks like this

print('''<http://example.org/#'''+synset_offset+'''><http://www.monnetproject.eu/lemon#lex_filenum> "'''+lex_filenum+'''".
<http://example.org/#'''+synset_offset+'''> <http://www.monnetproject.eu/lemon#ss_type> "'''+ss_type+'''".
<http://example.org/#'''+synset_offset+'''> <http://www.monnetproject.eu/lemon#gloss> "'''+gloss+'''".''')

but for some reason a linebreak occurs after '''+gloss+'''

and looks like this

<http://example.org/#09824747> <http://www.monnetproject.eu/lemon#lex_filenum> "18".
<http://example.org/#09824747> <http://www.monnetproject.eu/lemon#ss_type> "n".
<http://example.org/#09824747> <http://www.monnetproject.eu/lemon#gloss> " a person who is expert in the use of a bow and arrow
".

I want to remove that linebreak as it wont allow the text to be formatted otherwise

Upvotes: 0

Views: 106

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1121624

.split() without arguments or None as the first argument removes whitespace around the line first, but .split('|') does not.

Remove it explicitly before splitting:

L2 = line.strip().split('|')

or after:

gloss = L2[1].strip()

.strip() removes all leading and trailing whitespace. You can be more specific and only remove newlines from the end using `.rstrip():

gloss = L2[1].rstrip('\n')

Upvotes: 4

Related Questions