简体   繁体   中英

Convert text file to list in Python

im a beginer!

I have a text file like this:

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

I want to retrieve data starting from 2nd row assigned to the corresponding column automatic with the other file. How can i do that?

Ex:

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

Thank you so much!

import re
Spresence = []
with open('yourFile.txt') as FL:
    for line in FL.readlines()[1:]:
        m = re.match("\S+\s+\S+\s+\S+\s+(.*)", line)
        if m:
            Spresence+=re.split('\s+',m.group(1))

output

>>>Spresence
['0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '1', '0', '0', '1', '0', '0', '1', '0', '0', '1', '0', '0', '1', '0', '0', '1', '0', '0', '1', '0', '0', '1', '0', '0', '1', '0', '0', '1', '0', '0', '1', '0', '0', '1', '0', '0', '1', '0', '0', '1', '0', '0', '2', '1', '1', '1', '0', '0', '3', '1', '1', '1', '0', '0', '4', '1', '1', '1', '0', '1', '2', '1', '1', '1', '0', '0']

A bit hacky but it works:

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

Opens the file, creates an array, for each line in the file it splits the line per each space and then grabs the 5th element in the line which would be the SPresence column. It takes that element and adds it to the sprescence array and prints at the end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM