Reading from a text file python

前端 未结 4 1504
长情又很酷
长情又很酷 2021-01-24 13:38

I have a text file which contains a matrix of numbers:

999 999  10  8
3 4 999 999 999 
6 999 2 7 999
999 6 3 5  6
999 9 1 10 999
10 6 999 2 2 999
4条回答
  •  自闭症患者
    2021-01-24 13:58

    Using map() to get a list of lists:

    >>> with open('myfile.txt') as matrix:
    ...     [map(int, line.split()) for line in matrix]
    ...
    [[999, 999, 10, 8], [3, 4, 999, 999, 999], [6, 999, 2, 7, 999], [999, 6, 3, 5, 6], [999, 9, 1, 10, 999], [10, 6, 999, 2, 2, 999]]
    

提交回复
热议问题