New Python User
New Python User

Reputation: 23

Converting .txt data into two lists in Python

I have this data found in a .txt file:

    Round 1
Data Point 0: time=  0.0[hour], movement=   0.5[feet]
Data Point 1: time=  3.2[hour], movement=   5.54[feet]
Data Point 2: time= 10.1[hour], movement=   6.4[feet]
Data Point 3: time= 14.0[hour], movement=   7.02[feet]

+++++++++++++++++++++++++++++++++++++++++++++
    Round 2
Data Point 0: time=  0.0[hour], movement=  -5.2[feet]
Data Point 1: time=  2.3[hour], movement=   3.06[feet]
Data Point 2: time= 8.9[hour], movement=   4.07[feet]
Data Point 3: time= 9.4[hour], movement=   9.83[feet]

And, I would like to get the time and movement data and put them into two separate lists for Round 1 and Round 2. An example output:

time_1 = [0.0, 3.2, 10.1, 14.0]
movement_1 = [0.5, 5.54, 6.4, 7.02]

And, an identical format for Round 2. I know the general method of calling and opening the file with a with statement as well as using for and if statements to see what is in each line but, I don't quite know how to handle each Round's data separately as well as the separator +++++.

Upvotes: 0

Views: 88

Answers (4)

Padraic Cunningham
Padraic Cunningham

Reputation: 180391

If your file is exactly as posted:

import re

time_1 = []
movement_1 = []
time_2 = []
movement_2 = []
with open("in.txt") as f:
    for line in iter(lambda: f.readline().strip(),"+++++++++++++++++++++++++++++++++++++++++++++"): # keep going till the line "+++++++++++++++++++++++++++++++++++++++++++++"
        match = re.findall('\d+\.\d+|-\d+\.\d+', line)
        if match:
            time_1.append(match[0])
            movement_1.append(match[1])
    for line in f:       # move to lines after "+++++++++++++++++++++++++++++++++++++++++++++"
        match = re.findall('\d+\.\d+|-\d+\.\d+', line)
        if match:
            time_2.append(match[0])
            movement_2.append(match[1])

print time_1,movement_1
print time_2,movement_2
['0.0', '3.2', '10.1', '14.0'] ['0.5', '5.54', '6.4', '7.02']
['0.0', '2.3', '8.9', '9.4'] ['-5.2', '3.06', '4.07', '9.83']

If you want floats use time_1.append(float(match[0])) etc..

each sublist from each section will correspond to each other in times and movement

times = []
movements = []

with open("in.txt") as f:
    lines = f.read().split("+++++++++++++++++++++++++++++++++++++++++++++")
    for line in lines:
        match = re.findall('\d+\.\d+|-\d+\.\d+', line)         
        times.append(match[::2])
        movements.append(match[1::2])

If you have three rounds just unpack:

r1_times, r2_times, r3_times = times
r1_move, r2_move, r3_move = movements

print r1_times,r1_move
['0.0', '3.2', '10.1', '14.0'] ['0.5', '5.54', '6.4', '7.02']

Upvotes: 0

Patrick Roncagliolo
Patrick Roncagliolo

Reputation: 336

In this way you can create two lists with as many sublists as many rounds you have. you can obtain what you want by looking at the first, second sublist (first, second rounds) and more

with open("prova.txt","r") as f:       # put here the right filename
    Round = -1
    Times=[]
    Movements=[]
    for i in f.readlines(): 
        if "Round" in i:
            Round=Round+1
            Times.append([])
            Movements.append([])
        if i[0:4]=="Data":
            Times[Round].append(float(i.split("=")[1].split("[")[0]))
            Movements[Round].append(float(i.split("=")[2].split("[")[0]))
    print Times
    print Movements

    >>> [[0.0, 3.2, 10.1, 14.0], [0.0, 2.3, 8.9, 9.4]]      #Take a look to my results
    >>> [[0.5, 5.54, 6.4, 7.02], [-5.2, 3.06, 4.07, 9.83]]

    print Times[0]  #for times of first round
    print Times[1]  #for second round

... and so (it depends on how much rounds are in the text file)

Upvotes: 0

Tim Pietzcker
Tim Pietzcker

Reputation: 336128

You could first read your file, split it into rounds:

import re
with open("myfile.txt") as infile:
    rounds = re.split("\+{10,}", infile.read())

and then iterate over the rounds/lines:

result = []
for round in rounds:
    r = {"time":[], "move":[]}
    for match in re.findall(r"time=\s+(\d+\.\d+).*movement=\s+(-?\d+\.\d+)",
                            round):
        time, move = float(match[0]), float(match[1])
        r["time"].append(time)
        r["move"].append(move)
    result.append(r)

Result:

>>> result
[{'time': [0.0, 3.2, 10.1, 14.0], 'move': [0.5, 5.54, 6.4, 7.02]}, 
 {'time': [0.0, 2.3, 8.9, 9.4], 'move': [-5.2, 3.06, 4.07, 9.83]}]

Upvotes: 2

Camron_Godbout
Camron_Godbout

Reputation: 1633

This is a little dirty but it gives you two lists that contain a list for each round so time will be [time_1, time_2] and movement will be [movement_1, movement_2]

time = []
movement = []
totalTime = []
totalMovement = []
with open('data.txt') as f:
    for line in f:
        if line.find('+') == -1 and line.find('Round') == -1:
            tempTime = line[line.find('=')+1:line.find('[')]
            time.append(tempTime)
            tempMovement = line[line.find('t=')+2:line.find('[feet')]
            movement.append(tempMovement)
        elif line.find('+') != -1:
            totalTime.append(time)
            totalMovement.append(movement)
            time = []
            movement = []

Upvotes: 0

Related Questions