如何在Python中将行写入列表

这个问题在这里已经有了答案:

  • 在Python中,如何逐行读取文件到列表中? 35个答案

  • 使用open()打开文件并将其转换为如下所示的列表:

    with open("myfile.txt") as myfile:
        int_list = [int(l.strip()) for l in myfile]
    

    这遍历每一行,在最后剥离n ,转换为一个int ,并将其全部放入列表中。

    链接地址: http://www.djcxy.com/p/42359.html

    上一篇: How to write lines in python to a list

    下一篇: Pythons equivalent to PHP's file(fn, FILE