user2441441
user2441441

Reputation: 1387

python convert multiline to single line

I want to convert Python multiline string to a single line. If I open the string in a Vim , I can see ^M at the start of each line. How do I process the string to make it all in a single line with tab separation between each line. Example in Vim it looks like:

  Serialnumber
 ^MName           Rick
 ^MAddress           902, A.street, Elsewhere

I would like it to be something like:

Serialnumber \t Name \t Rick \t Address \t 902, A.street,......

where each string is in one line. I tried

   somestring.replace(r'\r','\t')

But it doesn't work. Also, once the string is in a single line if I wanted a newline(UNIX newline?) at the end of the string how would I do that?

Upvotes: 13

Views: 83365

Answers (8)

Jack Snowdon
Jack Snowdon

Reputation: 63

This should do the job:
string = """Name           Rick
 Address           902, A.street, Elsewhere"""

single_line = string.replace("\n", "\t")

Upvotes: 0

rouble
rouble

Reputation: 18201

I use splitlines() to detect all types of lines, and then join everything together. This way you don't have to guess to replace \r or \n etc.

"".join(somestring.splitlines())

Upvotes: 10

EladTal
EladTal

Reputation: 2846

this should do the work:

def flatten(multiline):
    lst = multiline.split('\n')
    flat = ''
    for line in lst:
        flat += line.replace(' ', '')+' '
    return flat

Upvotes: 0

Madiha
Madiha

Reputation: 21

it is hard coding. But it works.

poem='''
If I can stop one heart from breaking,
I shall not live in vain;
If I can ease one life the aching,
Or cool one pain,
Or help one fainting robin
Unto his nest again,
I shall not live in vain.
'''


lst=list(poem)
str=''
for i in lst:
    str+=i
print(str)
lst1=str.split("\n")
str1=""

for i in lst1:
    str1+=i+" "
str2=str1[:-2]
print(str2)

Upvotes: 2

Taohidul Islam
Taohidul Islam

Reputation: 5414

This trick also can be useful, write "\n" as a raw string. Like :

my_string = my_string.replace(r"\n", "\t")

Upvotes: 0

Vorticity
Vorticity

Reputation: 4928

Deleted my previous answer because I realized it was wrong and I needed to test this solution.

Assuming that you are reading this from the file, you can do the following:

f = open('test.txt', 'r')
lines = f.readlines()
mystr = '\t'.join([line.strip() for line in lines])

As ep0 said, the ^M represents '\r', which the carriage return character in Windows. It is surprising that you would have ^M at the beginning of each line since the windows new-line character is \r\n. Having ^M at the beginning of the line indicates that your file contains \n\r instead.

Regardless, the code above makes use of a list comprehension to loop over each of the lines read from test.txt. For each line in lines, we call str.strip() to remove any whitespace and non-printing characters from the ENDS of each line. Finally, we call '\t'.join() on the resulting list to insert tabs.

Upvotes: 13

Sébastien Bernery
Sébastien Bernery

Reputation: 186

You can replace "\r" characters by "\t".

my_string.replace("\r", "\t")

Upvotes: 7

ep0
ep0

Reputation: 710

This occurs of how VIM interprets CR (carriage return), used by Windows to delimit new lines. You should use just one editor (I personally prefer VIM). Read this: VIM ^M

Upvotes: 0

Related Questions