我有一个文本文件,其数据结构如下:
01/May/1998:15:28:53 test123 0 383L 281L 399
01/May/1998:14:23:28 doe821 62C 621L 379
01/May/1998:22:10:11 testABC 0 635R 407R 671R 671N 407N 407Q 407L 496L 569
每个数据都以日期和时间开头,格式如下:01/May/1998:15:28:53
。
我开始阅读文本文件,但现在我想将其转换为列表。我怎样才能做到这一点? 我需要正则表达式吗?
非常感谢任何帮助。
编辑: 我想要这个输出:
[
['01/May/1998:15:28:53', 'test123', '0', '383L', '281L', '399'],
['01/May/1998:14:23:28', 'doe821', '62C', '621L', '379'],
['01/May/1998:22:10:11', 'testABC', '0', '635R', '407R', '671R', '671N', '407N', '407Q', '407L', '496L', '569']
]
答案 0 :(得分:2)
在每一行上拨打str.split()
会给您:
['01/May/1998:15:28:53', 'test123', '0', '383L', '281L', '399']
如:
with open('textfile') as f:
for line in f:
print line.split()
['01/May/1998:15:28:53', 'test123', '0', '383L', '281L', '399']
['01/May/1998:14:23:28', 'doe821', '62C', '621L', '379']
['01/May/1998:22:10:11', 'testABC', '0', '635R', '407R', '671R', '671N', '407N', '407Q', '407L', '496L', '569']
将每一行作为一个列表项:
with open('textfile') as f:
print f.readlines() # note the newline chars(\n) that may need slicing off
['01/May/1998:15:28:53 test123 0 383L 281L 399\n', '01/May/1998:14:23:28 doe821 62C 621L 379\n', '01/May/1998:22:10:11 testABC 0 635R 407R 671R 671N 407N 407Q 407L 496L 569\n']
要将每一行拆分并放在一个大清单中:
with open('textfile') as f:
print [line.split() for line in f]
[['01/May/1998:15:28:53', 'test123', '0', '383L', '281L', '399'], ['01/May/1998:14:23:28', 'doe821', '62C', '621L', '379'], ['01/May/1998:22:10:11', 'testABC', '0', '635R', '407R', '671R', '671N', '407N', '407Q', '407L', '496L', '569']]
答案 1 :(得分:0)
假设您的文件名为test.data
>>> with open('test.data') as f:
>>> [x for x in [y.split() for y in f.read().split('\n')]]