繁体   English   中英

尝试用python中的新行(\\ n)替换“。”,“。”后没有空格

[英]trying to replace “.” with new line (\n) in python, without space after the “.”

我正在尝试替换“。” 在python中使用新行(“ \\ n”)。

input file:
abc. abc
def. def

new input:
Nigel Reuben Rook Williams (15 July 1944 – 21 April 1992) was an English conservator and expert on the restoration of ceramics and glass. From 1961 until his death he worked at the British Museum, where he became the Chief  successful restorations of the Sutton Hoo helmet and the Portland Vase.

Joining

码:

test_file=open(path,'r')
test_file_1=open(new_path,'w')

for line in test_file:
    test_file_1.write(line.replace('.','\n'))

test_file.close()
test_file_1.close()

我想删除新行之前的空格。

expected new output:
nigel reuben rook williams july april was an english  restoration of ceramics and glass
from until his death he worked at the british museum where he became the 
chief conservator of ceramics and glass in
there his work included the successful restorations of the sutton hoo helmet 
and the portland vase
joining

点后有空白。 您可以使用line.replace('. ','\\n')

或者您可以使用:

for line in test_file:
    words = line.split('.')
    for word in words:
        test_file_1.write(word.strip()+'\n')

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM