Python⽂件writelines()⽅法和处理双层列表概述
writelines() ⽅法⽤于向⽂件中写⼊⼀序列的字符串。
这⼀序列字符串可以是由迭代对象产⽣的,如⼀个字符串列表。
换⾏需要制定换⾏符 \n。
语法
writelines() ⽅法语法如下:
fileObject.writelines( [ str ])
data = ['a','b','c']
with open("","w") as f:
f.writelines(data)
输出:
writelines()方法将什么写入文件
对于双层列表中的数据
data = [ ['a','b','c'],['a','b','c'],['a','b','c']]
with open("","w") as f:
for i in data:  # 对于双层列表中的数据
i = str(i).strip('[').strip(']').replace(',', '').replace('\'', '') + '\n'# 将其中每⼀个列表规范化成字符串
f.write(i)
输出:
部分内容来⾃⽹络