Search code examples
pythonnumpygenfromtxt

How to selectively import only lines of words with genfromtxt into Python?


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

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