您可以执行以下操作:
[x.split() for x in fileobject if x.strip()]
要获取整数,您可以使用map:
[map(int,x.split()) for x in fileobject if x.strip()]
fileobject返回的对象在哪里open。这可能最好在上下文管理器中执行:
with open(filename) as fileobject:
data_list = [map(int,x.split()) for x in fileobject if x.strip()]
阅读其他帖子的一些评论,似乎我也没有正确理解您的问题。这是我纠正它的努力:
with open(filename) as fileobject:
current = []
result = [current]
for line in fileobject:
if line.strip(): #Non-blank line -- Extend current working list.
current.extend(map(int,line.split()))
else: #blank line -- Start new list to work with
current = []
result.append(current)
现在您的结果列表应该包含在result.