使用Python将文本文件中的空格转换为换行符
我有一个如下所示的文本文件:使用Python将文本文件中的空格转换为换行符,python,Python,我有一个如下所示的文本文件: 15.9 17.2 18.6 10.5 我想用Python编辑此文件,使其如下所示: 15.9 17.2 18.6 10.5 这意味着我需要用换行符字符串替换空格字符串并保存文本 我试过这个,但不起作用: f = open("testfile.txt", "w") for line in f: if ' ' in line: line2 = line.replace(' ' , '\n') print(line2) for i
15.9 17.2 18.6 10.5
我想用Python编辑此文件,使其如下所示:
15.9
17.2
18.6
10.5
这意味着我需要用换行符字符串替换空格字符串并保存文本
我试过这个,但不起作用:
f = open("testfile.txt", "w")
for line in f:
if ' ' in line:
line2 = line.replace(' ' , '\n')
print(line2)
for i in line2:
f.write(line2(i))
f.close
line2
的打印已经开始工作,但是我没有得到一个新的文本文件,其中空格被换行符替换
如何解决问题并生成所需的输出?请尝试这样做
with open("testfile.txt", "r") as r:
with open("testfile_new.txt", "w") as w:
w.write(r.read(.replace(' ' , '\n'))
f=open(“testfile.txt”、“r”)
text=f.read()
f、 关闭()
f=打开(“testfile.txt”,“w+”)
text2=''
如果文本中有“”:
text2=text.replace(“”,“\n”)
打印(文本2)
f、 写(文本2)
f、 关闭()
您可以尝试使用字符串替换:
首先:f.close()不存在
其次:尝试上面的代码。它将把空格替换为新行。示例:
with open("file1.txt", "r") as read_file:
with open("file2.txt", "w") as write_file:
write_file.write(read_file.read().replace(" ", '\n'))
15.9 17.2 18.6 10.5
15.9
17.2
18.6
10.5
with open("testfile.txt", "r") as infile:
data = infile.read()
with open("testfile.txt", "w") as outfile:
outfile.write("\n".join(data.split()))
file1.txt的内容:
with open("file1.txt", "r") as read_file:
with open("file2.txt", "w") as write_file:
write_file.write(read_file.read().replace(" ", '\n'))
15.9 17.2 18.6 10.5
15.9
17.2
18.6
10.5
with open("testfile.txt", "r") as infile:
data = infile.read()
with open("testfile.txt", "w") as outfile:
outfile.write("\n".join(data.split()))
file2.txt的内容:
with open("file1.txt", "r") as read_file:
with open("file2.txt", "w") as write_file:
write_file.write(read_file.read().replace(" ", '\n'))
15.9 17.2 18.6 10.5
15.9
17.2
18.6
10.5
with open("testfile.txt", "r") as infile:
data = infile.read()
with open("testfile.txt", "w") as outfile:
outfile.write("\n".join(data.split()))
注意:
with open("file1.txt", "r") as read_file:
with open("file2.txt", "w") as write_file:
write_file.write(read_file.read().replace(" ", '\n'))
15.9 17.2 18.6 10.5
15.9
17.2
18.6
10.5
with open("testfile.txt", "r") as infile:
data = infile.read()
with open("testfile.txt", "w") as outfile:
outfile.write("\n".join(data.split()))
或者您可以使用split
和join
方法代替replace
write_file.write("\n".join(read_file.read().split()))
使用
str.split
和str.join
Ex:
with open("file1.txt", "r") as read_file:
with open("file2.txt", "w") as write_file:
write_file.write(read_file.read().replace(" ", '\n'))
15.9 17.2 18.6 10.5
15.9
17.2
18.6
10.5
with open("testfile.txt", "r") as infile:
data = infile.read()
with open("testfile.txt", "w") as outfile:
outfile.write("\n".join(data.split()))
首先,您没有
f.close()
您的文件。其次,我相信您可以直接编写行f.write(line2)
,您不需要迭代它。此外,您有两个独立的for循环,在第一个for循环中,您不断覆盖line2
。因此,唯一写入的第2行是循环中的最后一行。注意,这会将整个文件拖到内存中。另外,您可以使用将open('testfile.txt','r')作为r,将('testfile_new.txt','w')作为w:
打开,并避免阻塞。还有一个语法错误:<代码> R.Read(.Read('','\n))应该是代码> Read Read()。和/或提供一些关于OP在其代码尝试中出错的内容的文字。这将不起作用!!!它将创建一个新文件,而不是OP想要的文件。您还可以尝试不同的组合,例如string.replace('\r','')。replace('\n','')