xxxxxxxxxx
line = "this is a text\n"
new_string = string.rstrip();
xxxxxxxxxx
# initialize list
test_list = ['gf\ng', 'i\ns', 'b\nest', 'fo\nr', 'geeks\n']
# printing original list
print("The original list : " + str(test_list))
# Removing newline character from string
# using loop
res = []
for sub in test_list:
res.append(sub.replace("\n", ""))
# printing result
print("List after newline character removal : " + str(res))
xxxxxxxxxx
# Remove all line breaks from a long string of text
mystr = 'hello world, how do i enter line breaks?'
>>> mystr.replace(' ', '')
'helloworld,howdoienterlinebreaks?'
# You can also replace more then one thing for example:
mystring = mystring.replace('\n', ' ').replace('\r', '')
xxxxxxxxxx
list1 = ["Starbucks\n", "has the \nbest", "coffee\n\n "]
rez = []
for x in list1:
rez.append(x.replace("\n", ""))
print("New list : " + str(rez))
xxxxxxxxxx
mylist = []
# Assuming that you have loaded data into a lines variable.
for line in lines:
mylist.append(line.strip().split('\t')