[英]How to delete empty space from a text file using python?
您只需要讀入文件,刪除新行並再次寫入即可。
with open("./foo.txt", "r") as f:
formatted = ""
for line in f.readlines():
formatted += line.replace('\n', " ") # Removes the new lines, add spaces instead
formatted.replace(" ", " ") # Replace double space with one space
# Writes single line to textfile
with open("./bar.txt", "w") as out:
out.write(formatted)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.