在Python中将文本文件转换为列表

时间:2015-09-22 16:58:53

标签: python

我是一个初学者!

我有一个这样的文本文件:

dates sampletime occupancy SActivities SPresence KhPresence AuPresence CarloPresence
04/05/2015 00:00:00 1430690400  0   0   0   0   0   0
04/05/2015 00:30:00 1430692200  0   0   0   0   0   0
04/05/2015 01:00:00 1430694000  0   0   0   0   0   0
04/05/2015 01:30:00 1430695800  0   0   0   0   0   0
04/05/2015 02:00:00 1430697600  0   0   0   0   0   0
04/05/2015 02:30:00 1430699400  0   0   0   0   0   0
04/05/2015 03:00:00 1430701200  0   0   0   0   0   0
04/05/2015 03:30:00 1430703000  0   0   0   0   0   0
04/05/2015 04:00:00 1430704800  0   0   0   0   0   0
04/05/2015 04:30:00 1430706600  0   0   0   0   0   0
04/05/2015 05:00:00 1430708400  0   0   0   0   0   0
04/05/2015 05:30:00 1430710200  0   0   0   0   0   0
04/05/2015 06:00:00 1430712000  0   0   0   0   0   0
04/05/2015 06:30:00 1430713800  0   0   0   0   0   0
04/05/2015 07:00:00 1430715600  0   0   0   0   0   0
04/05/2015 07:30:00 1430717400  0   0   0   0   0   0
04/05/2015 08:00:00 1430719200  0   0   0   0   0   0
04/05/2015 08:30:00 1430721000  0   0   0   0   0   0
04/05/2015 09:00:00 1430722800  0   0   0   0   0   0
04/05/2015 09:30:00 1430724600  0   0   0   0   0   0
04/05/2015 10:00:00 1430726400  1   0   0   1   0   0
04/05/2015 10:30:00 1430728200  1   0   0   1   0   0
04/05/2015 11:00:00 1430730000  1   0   0   1   0   0
04/05/2015 11:30:00 1430731800  1   0   0   1   0   0
04/05/2015 12:00:00 1430733600  1   0   0   1   0   0
04/05/2015 12:30:00 1430735400  1   0   0   1   0   0
04/05/2015 13:00:00 1430737200  1   0   0   1   0   0
04/05/2015 13:30:00 1430739000  2   1   1   1   0   0
04/05/2015 14:00:00 1430740800  3   1   1   1   0   0
04/05/2015 14:30:00 1430742600  4   1   1   1   0   1
04/05/2015 15:00:00 1430744400  2   1   1   1   0   0

我想从另一个文件中检索分配给相应列自动的第二行开始检索数据。我怎么能这样做?

前:

Spresence=['0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0',...]

非常感谢你!

2 个答案:

答案 0 :(得分:0)

{{1}}

输出

{{1}}

答案 1 :(得分:0)

有点hacky但它​​有效:

import os
file = open("file.txt", 'r')
spresence = []
for line in file.readlines():
        splitLine = line.split(" ")
        if len(splitLine) < 5:
                continue
        if splitLine[4] == "SPresence":
                continue
        spresence.append(splitLine[4])
print spresence

打开文件,创建一个数组,对于文件中的每一行,它按每个空格分割行,然后抓取行中的第5个元素,即SPresence列。它接受该元素并将其添加到sprescence数组并在末尾打印