如何在python中将文件的内容写入列表?

sha*_*ath 3 python nested-lists python-2.7

我正在学习python编程并且遇到了这个问题.我查看了其他读取文件输入的示例,并将整个事件作为单个列表或作为该示例的字符串链接, 但我希望每行都是一个列表(嵌套列表)我该怎么做请帮助
文本文件是a.txt

1234 456 789 10 11 12 13

4456 585 568 2 11 13 15 
Run Code Online (Sandbox Code Playgroud)

代码的输出必须是这样的

[ [1234 456 789 10 11 12 13],[4456 585 568 2 11 13 15] ]
Run Code Online (Sandbox Code Playgroud)

Ada*_*ith 5

没理由readlines- 只是迭代文件.

with open('path/to/file.txt') as f:
    result = [line.split() for line in f]
Run Code Online (Sandbox Code Playgroud)

如果需要一个int列表列表:

with open('path/to/file.txt') as f:
    result = [map(int, line.split()) for line in f]
    # [list(map(int, line.split())) for line in f] in Python3
Run Code Online (Sandbox Code Playgroud)

  • @BhargavRao我今天真的被代表上限:P (2认同)