Question

I have a data text file which has the following format:

1 2 2 3 4 5 6
1 5 8 9 3 4 2
1 2 3 5 1 2 3     
Timestamp 1   
5 4 8 9 8 7 2 
1 5 9 6 3 1 2
Timestamp 2
...

I wish to import the data in such a way that:

  1. I can ignore the timestamps first and process the data.
  2. AND I can also handle the timestamps later.

I have achieved 1 by

myData = np.genfromtxt('data.txt', comments='T')

By doing so, I have the following in Python

    1 2 2 3 4 5 6
    1 5 8 9 3 4 2
    1 2 3 5 1 2 3 
    5 4 8 9 8 7 2 
    1 5 9 6 3 1 2

However, by doing this I simply have discarded all the timestamps.

But I need to process them later as well.

How may I import the timestamps into another list like below in Python?

Timestamp 1
Timestamp 2
...
Was it helpful?

Solution

How about this?

I assume that the numbers before a Timestamp are the ones belong to it:

This snippet also converts the numbers to integers.

CODE:

with open('source.txt', 'r') as f:
    data = {}
    numbers = []
    for line in f:
        ln = line.strip()
        if 'Timestamp' in ln:
            data[ln] = numbers
            numbers = []
        elif ln:
            numbers.append([int(n) for n in ln.split()])

print(data)

OUTPUT:

{
    'Timestamp 2':
    [
        [5, 4, 8, 9, 8, 7, 2],
        [1, 5, 9, 6, 3, 1, 2]
    ],
    'Timestamp 1':
    [
        [1, 2, 2, 3, 4, 5, 6],
        [1, 5, 8, 9, 3, 4, 2],
        [1, 2, 3, 5, 1, 2, 3]
    ]
}

OTHER TIPS

@PeterVaro has a nice solution that keeps the timestamps linked to the data, but if all you want to do is import the numbers and timestamps into separate lists, you can do this:

with open('data.txt') as dataFile:
    numbers = []
    timestamps = []
    for line in dataFile:
        # if statement makes sure it's not a blank line with only a newline character in it.
        if len(line) > 1:
            if 'Timestamp' in line:
                timestamps.append(line.rstrip())
            else:
                numbers.append([int(x) for x in line.split()])

for line in numbers:
    for number in line:
        print number, " ",
    print

print

for timestamp in timestamps:
    print timestamp

output:

1   2   2   3   4   5   6  
1   5   8   9   3   4   2  
1   2   3   5   1   2   3  
5   4   8   9   8   7   2  
1   5   9   6   3   1   2  

Timestamp 1
Timestamp 2
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top