[英]My .writelines() function is not working in python
我是 Python 的新手(以及一般的編碼)。 所以,請幫我解決這個問題。
代碼:
f = open("info.txt", "w")
l = ["Apples", "Android", "Coffee"]
f.writelines(l)
f.close()
f = open("info.txt", "r")
s = f.read()
print(s)
輸出如下所示:
或者:
ApplesAndroidCoffee
Process finished with exit code 0
writelines
不會為您提供換行符,因此如果字符串不包含它們,那么您將不得不添加它們。
你可以這樣做:
f.writelines(line + '\n' for line in l)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.