python将多行转换为单行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18004807/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
python convert multiline to single line
提问by user2441441
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:
我想将 Python 多行字符串转换为单行。如果我在 Vim 中打开字符串,我可以在每一行的开头看到 ^M 。如何处理字符串以使其全部在一行中,每行之间有制表符分隔。Vim 中的示例如下所示:
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?
但它不起作用。另外,一旦字符串在一行中,如果我想在字符串末尾添加换行符(UNIX 换行符?),我该怎么做?
采纳答案by Vorticity
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.
正如 ep0 所说,^M 代表 '\r',这是 Windows 中的回车符。由于 windows 换行符是 \r\n,所以在每行的开头都有 ^M 令人惊讶。在行的开头有 ^M 表示您的文件包含 \n\r 。
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.
无论如何,上面的代码使用列表理解来循环从test.txt
. 对于每个line
in lines
,我们调用str.strip()
从每行的 ENDS 中删除任何空格和非打印字符。最后,我们调用'\t'.join()
结果列表插入选项卡。
回答by ep0
回答by Sébastien Bernery
You can replace "\r" characters by "\t".
您可以用“\t”替换“\r”字符。
my_string.replace("\r", "\t")
回答by Taohidul Islam
This trick also can be useful, write "\n"
as a raw string
. Like :
这招也可以是有用的,写"\n"
的raw string
。喜欢 :
my_string = my_string.replace(r"\n", "\t")
回答by Madiha
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)