Python函数【008】——保存list到文件

2020-11-10
#Python
#!/usr/local/bin/python3
# -*- coding: utf-8 -*-  
"""
Date: 2019/11/28
Author: Xiao-Le Deng
Function: write a list into the file
"""

def write_list_to_file(list,file):
    """write a list into the file, 'list' is the list and 'file' is the saved file"""
    with open(file,'w') as f:
        for i,j in enumerate(list):
            x = str(j)+'\n'
            f.write(x)

# example
# l =[1,2,3,4,5]
# l =['a','d',1,2,4]
# write_list_to_file(l,"writefile.txt")