How to convert multiline string to single line in python

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?

asked Aug 1, 2013 at 21:21

user2441441user2441441

1,2314 gold badges23 silver badges40 bronze badges

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.

answered Aug 1, 2013 at 23:52

VorticityVorticity

4,1342 gold badges28 silver badges46 bronze badges

8

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

my_string.replace["\r", "\t"]

answered Aug 1, 2013 at 21:27

1

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[]]

answered Dec 24, 2020 at 18:21

roublerouble

14.8k16 gold badges101 silver badges97 bronze badges

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]

Vlad

7,6775 gold badges28 silver badges44 bronze badges

answered Mar 24, 2019 at 15:40

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

answered Aug 1, 2013 at 21:24

ep0ep0

7125 silver badges13 bronze badges

1

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

my_string = my_string.replace[r"\n", "\t"]

answered Feb 8, 2019 at 9:16

Taohidul IslamTaohidul Islam

5,1003 gold badges23 silver badges38 bronze badges

this should do the work:

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

answered Dec 23, 2020 at 21:15

EladTalEladTal

1,5771 gold badge14 silver badges9 bronze badges

This should do the job:

string = """Name           Rick
 Address           902, A.street, Elsewhere"""

single_line = string.replace["\n", "\t"]

answered May 17, 2021 at 16:46

How do you convert multiple lines to one line?

Select the lines you want to join [ Ctrl + A to select all] Choose Edit -> Line Operations -> Join Lines.

How do you convert a multi line string to a list in Python?

Python: Split a multiline string into a list of lines.
Use str. split[] and '\n' to match line breaks and create a list..
str. splitlines[] provides similar functionality to this snippet..

How do I make one line of a string in Python?

The new line character in Python is \n . It is used to indicate the end of a line of text. You can print strings without adding a new line with end = , which is the character that will be used to separate the lines.

How do you break a long string in Python?

Use a backslash [ \ ] as a line continuation character In Python, a backslash [ \ ] is a line continuation character. If a backslash is placed at the end of a line, it is considered that the line is continued on the next line.

Chủ Đề