Python 从一长串文本中删除所有换行符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16566268/
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
Remove all line breaks from a long string of text
提问by Ian Zane
Basically, I'm asking the user to input a string of text into the console, but the string is very long and includes many line breaks. How would I take the user's string and delete all line breaks to make it a single line of text. My method for acquiring the string is very simple.
基本上,我要求用户在控制台中输入一串文本,但该字符串很长并且包含许多换行符。我将如何获取用户的字符串并删除所有换行符以使其成为一行文本。我获取字符串的方法非常简单。
string = raw_input("Please enter string: ")
Is there a different way I should be grabbing the string from the user? I'm running Python 2.7.4 on a Mac.
我应该以不同的方式从用户那里获取字符串吗?我在 Mac 上运行 Python 2.7.4。
P.S. Clearly I'm a noob, so even if a solution isn't the most efficient, the one that uses the most simple syntax would be appreciated.
PS 显然我是一个菜鸟,所以即使解决方案不是最有效的,使用最简单语法的解决方案也会受到赞赏。
采纳答案by Daren Thomas
How do you enter line breaks with raw_input? But, once you have a string with some characters in it you want to get rid of, just replacethem.
你如何输入换行符raw_input?但是,一旦你有一个包含一些字符的字符串,你想要摆脱replace它们,就只有它们。
>>> mystr = raw_input('please enter string: ')
please enter string: hello world, how do i enter line breaks?
>>> # pressing enter didn't work...
...
>>> mystr
'hello world, how do i enter line breaks?'
>>> mystr.replace(' ', '')
'helloworld,howdoienterlinebreaks?'
>>>
In the example above, I replaced all spaces. The string '\n'represents newlines. And \rrepresents carriage returns (if you're on windows, you might be getting these and a second replacewill handle them for you!).
在上面的示例中,我替换了所有空格。该字符串'\n'表示换行符。并\r代表回车(如果你在 Windows 上,你可能会得到这些,然后replace会为你处理它们!)。
basically:
基本上:
# you probably want to use a space ' ' to replace `\n`
mystring = mystring.replace('\n', ' ').replace('\r', '')
Note also, that it is a bad idea to call your variable string, as this shadows the module string. Another name I'd avoid but would love to use sometimes: file. For the same reason.
另请注意,调用您的变量是一个坏主意string,因为这会影响模块string。我会避免使用但有时会喜欢使用的另一个名称:file. 出于同样的原因。
回答by Konstantin Dinev
You can try using string replace:
您可以尝试使用字符串替换:
string = string.replace('\r', '').replace('\n', '')
回答by tokhi
回答by Sean
You can split the string with no separator arg, which will treat consecutive whitespace as a single separator (including newlines and tabs). Then join using a space:
您可以拆分不带分隔符 arg 的字符串,这会将连续的空格视为单个分隔符(包括换行符和制表符)。然后使用空格加入:
In : " ".join("\n\nsome text \r\n with multiple whitespace".split())
Out: 'some text with multiple whitespace'
回答by Kamil Neczaj
A method taking into consideration
一种方法考虑
- additional white characters at the beginning/end of string
- additional white characters at the beginning/end of every line
- various end-line characters
- 字符串开头/结尾的附加白色字符
- 每行开头/结尾的附加白色字符
- 各种结束行字符
it takes such a multi-line string which may be messy e.g.
它需要这样一个多行的字符串,这可能会很乱,例如
test_str = '\nhej ho \n aaa\r\n a\n '
and produces nice one-line string
并产生漂亮的单行字符串
>>> ' '.join([line.strip() for line in test_str.strip().splitlines()])
'hej ho aaa a'
UPDATE: To fix multiple new-line character producing redundant spaces:
更新:修复多个产生冗余空格的换行符:
' '.join([line.strip() for line in test_str.strip().splitlines() if line.strip()])
This works for the following too
test_str = '\nhej ho \n aaa\r\n\n\n\n\n a\n '
这也适用于以下情况
test_str = '\nhej ho \n aaa\r\n\n\n\n\n a\n '
回答by Neil
Another option is regex:
另一种选择是正则表达式:
>>> import re
>>> re.sub("\n|\r", "", "Foo\n\rbar\n\rbaz\n\r")
'Foobarbaz'
回答by Ankit Dwivedi
The problem with rstrip is that it does not work in all cases (as I myself have seen few). Instead you can use - text= text.replace("\n"," ") this will remove all new line \n with a space.
rstrip 的问题在于它并不适用于所有情况(因为我自己很少看到)。相反,您可以使用 - text= text.replace("\n"," ") 这将删除所有带有空格的新行 \n 。
Thanks in advance guys for your upvotes.
在此先感谢大家的投票。

