How can I store from a file to array in Python?

This question already has an answer here:

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

  • This is one way to achieve that:

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

    If you want your list to contain the numbers ( int ) instead of strings the following code will achieve this:

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

    Thanks to Ninja Puppy♦ to improve the code.

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

    上一篇: 从python文件中逐行读取

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