以下行将创建一个列表,其中每个项目都是一个列表。内部列表是一行,分为“单词”。
li = [i.strip().split() for i in open("input.txt").readlines()]
我将您发布的代码片段放入 c:\temp 中的 input.txt 文件中并运行此行。输出结果与你想要的相似吗?
C:\temp>python
Python 3.1.1 (r311:74483, Aug 17 2009, 17:02:12) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> print([i.strip().split() for i in open("input.txt").readlines()])
[['p', 'wfgh', '1111', '11111', '111111'], ['287', '48', '0'], ['65626', '-1818', '0'], ['4654', '21512', '02020', '0']]