我有一个文件,当我打开它时,它会打印出一些段落。我需要将这些段落与空格连接在一起以形成一大段文本。
例如
for data in open('file.txt'):
print data
有这样的输出:
Hello my name is blah. What is your name?
Hello your name is blah. What is my name?
输出怎么会是这样的?:
Hello my name is blah. What is your name? Hello your name is blah. What is my name?
我试过用这样的空格替换换行符:
for data in open('file.txt'):
updatedData = data.replace('\n',' ')
但这只会摆脱空行,不会加入段落
并尝试像这样加入:
for data in open('file.txt'):
joinedData = " ".join(data)
但这用空格分隔每个字符,同时也没有摆脱段落格式。