我如何从一个文件存储到Python中的数组?

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

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

  • 这是实现这一目标的一种方式:

    with open('filename') as file:
      lines = [i.strip() for i in file]
    

    如果你想让你的列表包含数字( int )而不是字符串,下面的代码将实现这一点:

    with open('seq.txt') as f:
      numbers = [int(i) for i in f]
    

    感谢Ninja Puppy♦改进代码。

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

    上一篇: How can I store from a file to array in Python?

    下一篇: Python: put each line of words in a list