python将文本中的空格替换为换行的方法

```python

# -*- coding: utf-8 -*-
'''
遇到文中的空格就换行
'''
def delblankline(infile, outfile):
infopen = open(infile, 'r',encoding="utf-8")
outfopen = open(outfile, 'w',encoding="utf-8")
db = infopen.read()
outfopen.write(db.replace(' ','\n'))
infopen.close()
outfopen.close()

delblankline("jb51.txt", "o3.txt")

```

Python3 replace()方法

描述
replace() 方法把字符串中的 old(旧字符串) 替换成 new(新字符串),如果指定第三个参数max,则替换不超过 max 次。

posted @ 2021-09-28 09:17  ArdenWang  阅读(822)  评论(0编辑  收藏  举报