[英]how do i remove line breakes from a text file in python so its easier to work with
我正在使用 python 3.8。 我的代碼仍然打印換行符。 我嘗試過函數strip()
、 rstrip()
和replace()
。
這是我的代碼的一個版本:
with open('code.txt', 'r') as c:
code = c.readlines()
code = ''.join(code)
print(code.replace("/n", ""))
代碼.txt是
hello
there
它的印刷
hello
there
但我想讓它打印
hellothere
你應該使用.strip()
方法
with open('code.txt', 'r') as c:
code = [i.strip() for i in c.readlines()]
code = ''.join(code)
>>> code
helloworld
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.