python3.*版本将list写入txt文件中

      要把list类型数据写入txt文件中,步骤如下:

step1 :open()一个要写入的文件

        with open("path\filename","wb") as f:

step2 : 读取list中的每一项

           for list_mem in list:

                   f.write(list_mem+"\n",encoding = "utf-8")

需要注意的是一定要在write()方法中加上参数encoding,否则会报错:Error:a bytes-like object is required not str

如果是在python2.7版本下,那么在使用write()方法时不能使用encoding = "utf-8"这种键值对的方式,否则报错

TypeError: write() takes no keyword arguments。

你可能感兴趣的:(Python基础)