FWQ
Python中如何避免writelines()函数并在文件中打印带有换行符的列表?
Python中如何避免writelines()函数并在文件中打印带有换行符的列表? 在文件中打印带有换行符的列表 要将列表写入文件时插入换行符,不能使用 writelines() 函数,因为它无法自动插入换行符。 解决方案 要实现所需行为,可以使用以下方法之一: 1. 循环写入: with open('your_file.txt', 'w') as f: for line in lines: f.write(f"{line}\n") 对于 python 3.6 之前的版本: with open('your_file.txt', 'w') as f:…