How to write lines in python to a list

This question already has an answer here:

  • In Python, how do I read a file line-by-line into a list? 35 answers

  • Open the file using open() and convert into a list like so:

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

    This iterates over every line, strips the n at the end, converts into an int , and puts it all into a list.

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

    上一篇: 在Python2.7中导入一个文本文件

    下一篇: 如何在Python中将行写入列表