0

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 +++++.

1
  • 5
    Show us what you have tried in Python. Commented Aug 4, 2014 at 13:19

4 Answers 4

2

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]}]
Sign up to request clarification or add additional context in comments.

Comments

0

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)

7 Comments

@NewPythonUser look at my exaple code if you want to work with more rounds without adding code... I can't comment other posts because i'm new user
You code is close to what I was wanting but when I run it the lists show up empty. I can't quite figure out why.
I copied in a txt file the text you posted in your question... are you sure you are using it? or can you give me the code that you use to create that txt file (if you have it)?
are you sure you have write the right filename at the beginning of my code?
Yes, I'm using the correct file name and the lists are the exact length that I need them (as if the numbers are there) but the numbers are not in the lists.
|
0

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 = []

Comments

0

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']

2 Comments

It works perfectly, thank you! So if I have several more Rounds would I repeat the second if statement for each one, just replacing the lists names (time_2, movement_2) when using append?
so you have multiple rounds separated by ++++++++++++++...?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.