在Python中将多行字符串作为一行读取
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18369441/
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
Read a multi-line string as one line in Python
提问by user2662091
I am writing a program that analyzes a large directory text file line-by-line. In doing so, I am trying to extract different parts of the file and categorize them as 'Name', 'Address', etc. However, due to the format of the file, I am running into a problem. Some of the text i have is split into two lines, such as:
我正在编写一个程序来逐行分析一个大目录文本文件。在这样做时,我试图提取文件的不同部分并将它们归类为“名称”、“地址”等。但是,由于文件的格式,我遇到了一个问题。我的一些文本被分成两行,例如:
'123 ABCDEF ST
APT 456'
How can I make it so that even through line-by-line analysis, Python returns this as a single-line string in the form of
我怎样才能做到,即使通过逐行分析,Python 也将其作为单行字符串以以下形式返回
'123 ABCDEF ST APT 456'
?
'123 ABCDEF ST APT 456'
?
回答by PyNEwbie
Assuming you are using windows if you do a print of the file to your screen you will see
假设您使用的是 Windows,如果您将文件打印到屏幕上,您将看到
'123 ABCDEF ST\nAPT 456\n'
the \n
represent the line breaks.
该\n
代表换行符。
so there are a number of ways to get rid of the new lines in the file. One easy way is to split the string on the newline characters and then rejoin the items from the list that will be created when you do the split
所以有很多方法可以去掉文件中的新行。一种简单的方法是在换行符上拆分字符串,然后重新加入拆分时将创建的列表中的项目
myList = [item for item in myFile.split('\n')]
newString = ' '.join(myList)
回答by theodox
if you want to remove newlines:
如果要删除换行符:
"".join( my_string.splitlines())
回答by g.d.d.c
Assuming you're iterating through your file with something like this:
假设您正在使用以下内容迭代您的文件:
with open('myfile.txt') as fh:
for line in fh:
# Code here
And also assuming strings in your text file are delimited with single quotes, I would do this:
并且假设您的文本文件中的字符串用单引号分隔,我会这样做:
while not line.endswith("'"):
line += next(fh)
That's a lot of assuming though.
不过,这是很多假设。
回答by Kevin London
To replace the newlines with a space:
用空格替换换行符:
address = '123 ABCDEF ST\nAPT 456\n'
address.replace("\n", " ")
回答by perreal
import re
def mergeline(c, l):
if c: return c.rstrip() + " " + l
else: return l
def getline(fname):
qstart = re.compile(r'^\'[^\']*$')
qend = re.compile(r'.*\'$')
with open(fname) as f:
linecache, halfline = ("", False)
for line in f:
if not halfline: linecache = ""
linecache = mergeline(linecache, line)
if halfline: halfline = not re.match(qend, line)
else: halfline = re.match(qstart, line)
if not halfline:
yield linecache
if halfline:
yield linecache
for line in getline('input'):
print line.rstrip()
回答by Mehul
i think i might have found a easy solution just put .replace('\n', " ")
to whatever string u want to convert
我想我可能已经找到了一个简单的解决方案,只需将其放入.replace('\n', " ")
您想要转换的任何字符串
Example u have
你有的例子
my_string = "hi i am an programmer\nand i like to code in python"
like anything and if u want to convert it u can just do
喜欢任何东西,如果你想转换它,你可以做
my_string.replace('\n', " ")
hope it helps
希望能帮助到你