I have the following Codesample:
Codesample:
import string
import fileinput
Path = '/Volumes/test_copy.txt'
#/I
filename = Path
with open(filename) as file_object:
lines = file_object.readlines()
mylists = [item + "ENDTOKEN" + for item in lines]
mylists2 = ["STARTTOKEN" + item + "ENDTOKEN" + for item in lines]
#/O
print(mylists)
print(mylists2)
Content of test_copy.txt:
textprompt nextelement
Output:
textprompt nextelement
ENDTOKEN
STARTTOKENtextprompt nextelement
ENDTOKEN
Preferred Output:
STARTTOKENtextprompt nextelementENDTOKEN # 1. Pref.
STARTTOKENtextprompt nextelement ENDTOKEN # 2. Pref.
So far the newline only occurs in the after '+' So the Python mechanism of newlines.
Is there anyway to change the list properties? I'm also aware of the same issue for print(), but i know how to fix it there with more '''.
Edit: However, I'd like to have the lists cleaner. Printing out list[0] does print it also with \n
I have also tryed conditions, behind the comprehension with "\n" or " ".
Changed 'list' to ‘mylists‘. Thx to rockzxm & BeRT2me here.
Solved by BeRT2me
Thanks in advance!