Python import text file where each line has different number of columns

jpmorr

I'm new to python and I'm trying to figure out how to load a data file that contains blocks of data on a per timestep basis, such as like this:

TIME:,0
Q01 : A:,-10.7436,0.000536907,-0.00963283,0.00102934
Q02 : B:,0,0.0168694,-0.000413983,0.00345921
Q03 : C:,0.0566665
Q04 : D:,0.074456
Q05 : E:,0.077456
Q06 : F:,0.0744835
Q07 : G:,0.140448
Q08 : H:,-0.123968
Q09 : I:,0
Q10 : J:,0.00204377,0.0109621,-0.0539183,0.000708574
Q11 : K:,-2.86115e-17,0.00947104,0.0145645,1.05458e-16,-1.90972e-17,-0.00947859
Q12 : L:,-0.0036781,0.00161254
Q13 : M:,-0.00941257,0.000249692,-0.0046302,-0.00162387,0.000981709,-0.0135982,-0.0223496,-0.00872062,0.00548815,0.0114075,.........,-0.00196206
Q14 : N:,3797, 66558
Q15 : O:,0.0579981
Q16 : P:,0
Q17 : Q:,625

TIME:,0.1
Q01 : A:,-10.563,0.000636907,-0.00963283,0.00102934
Q02 : B:,0,0.01665694
Q03 : C:,0.786,-0.000666,0.6555
Q04 : D:,0.87,0.96
Q05 : E:,0.077456
Q06 : F:,0.07447835
Q07 : G:,0.140448
Q08 : H:,-0.123968
Q09 : I:,0
Q10 : J:,0.00204377,0.0109621,-0.0539183,0.000708574
Q11 : K:,-2.86115e-17,0.00947104,0.0145645,1.05458e-16,-1.90972e-17,-0.00947859
Q12 : L:,-0.0036781,0.00161254
Q13 : M:,-0.00941257,0.000249692,-0.0046302,-0.00162387,0.000981709,-0.0135982,-0.0223496,-0.00872062,0.00548815,0.0114075,.........,-0.00196206
Q14 : N:,3797, 66558
Q15 : O:,0.0579981
Q16 : P:,0,2,4
Q17 : Q:,786

Each block contains a number of variables that may have very different numbers of columns of data in it. The number of columns per variable may change in each timestep block, but the number of variables per block is the same in every timestep and it is always known how many variables were exported. There is no information on the number of blocks of data (timesteps) in the data file.

When the data has been read, it should be loaded in a format of variable per timestep:

Time:  |  A:                                           |  B:
0      |  -10.7436,0.000536907,-0.00963283,0.00102934  |  ........
0.1    |  -10.563,0.000636907,-0.00963283,0.00102934   |  ........
0.2    |  ......                                       |  ........

If the number of columns of data was the same every timestep and the same for every variable , this would be a very simple problem.

I guess I need to read the file line by line, in two loops, one per block and then once inside each block and then store the inputs in an array (append?). The changing number of columns per line has me a little stumped at the minute since I'm not very familiar with python and numpy yet.

If someone could point me in the right direction, such as what functions I should be using to do this relatively efficiently, that would be great.

VelikiiNehochuha
import pandas as pd
res = {}
TIME = None

# by default lazy line read
for line in open('file.txt'):
    parts = line.strip().split(':')
    map(str.strip, parts)
    if len(parts) and parts[0] == 'TIME':
        TIME = parts[1].strip(',')
        res[TIME] = {}
        print('New time section start {}'.format(TIME))
        # here you can stop and work with data from previou period
        continue

    if len(parts) <= 1:
        continue
    res[TIME][parts[1].lstrip()] = parts[2].strip(',').split(',')

df = pd.DataFrame.from_dict(res, 'columns')
# for example for TIME 0
dfZero = df['0']
print(dfZero)


df = pd.DataFrame.from_dict(res, 'index')

dfA = df['A']
print(dfA)

enter image description here

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Python import text file where each line has different number of columns

From Dev

Number of same line in each text file

From Dev

populate table where each column comes from a different line of a text file

From Dev

populate table where each column comes from a different line of a text file

From Dev

How to save multiple output in multiple file where each file has a different title coming from an object in python?

From Dev

How to run a command through a text file with different number of arguments on each line

From Dev

Streamreader initialization with a text file that has an empty line each other line

From Dev

Reading a Multi-line text file with different data in each line

From Dev

How to parse a csv file where each row has multiple columns?

From Dev

Count values for each line in text file python

From Dev

Print the line number of a text file for each word from a list in python 3 without importing anything

From Dev

Reading each line form a text file and determining if it has another letter

From Dev

CSV file with different number of records each line (CSV Reader)

From Dev

Printing out each line (from file) with line number python

From Dev

Different number of columns for each row

From Dev

MatLab - How to read a text file separated by ';', with different number of columns

From Dev

Add each character from each line of a text file, to a list in Python

From Dev

How to efficiently read lines of large text file in Python in different orders: a) random line each time, b) starting in middle...?

From Dev

python: generate different file number sequence for each function

From Dev

Split csv file by number of fields (each line different) to different target endpoints

From Dev

python - How to extract strings from each line in text file?

From Dev

How to retrieve each line of a text file in order [python 3]

From Dev

Different Where Clause for each selected columns

From Dev

How can I prepend a line number and tab to each line of a text file?

From Dev

Different text file for each language

From Dev

How to read text file with uneven number of columns with python-pandas?

From Dev

Adding file path to each line of line of text

From Dev

Julia: How to import a graph from text file (csv with unequal number of 'columns')?

From Dev

Comparing each line in txt file to another text file in each line

Related Related

  1. 1

    Python import text file where each line has different number of columns

  2. 2

    Number of same line in each text file

  3. 3

    populate table where each column comes from a different line of a text file

  4. 4

    populate table where each column comes from a different line of a text file

  5. 5

    How to save multiple output in multiple file where each file has a different title coming from an object in python?

  6. 6

    How to run a command through a text file with different number of arguments on each line

  7. 7

    Streamreader initialization with a text file that has an empty line each other line

  8. 8

    Reading a Multi-line text file with different data in each line

  9. 9

    How to parse a csv file where each row has multiple columns?

  10. 10

    Count values for each line in text file python

  11. 11

    Print the line number of a text file for each word from a list in python 3 without importing anything

  12. 12

    Reading each line form a text file and determining if it has another letter

  13. 13

    CSV file with different number of records each line (CSV Reader)

  14. 14

    Printing out each line (from file) with line number python

  15. 15

    Different number of columns for each row

  16. 16

    MatLab - How to read a text file separated by ';', with different number of columns

  17. 17

    Add each character from each line of a text file, to a list in Python

  18. 18

    How to efficiently read lines of large text file in Python in different orders: a) random line each time, b) starting in middle...?

  19. 19

    python: generate different file number sequence for each function

  20. 20

    Split csv file by number of fields (each line different) to different target endpoints

  21. 21

    python - How to extract strings from each line in text file?

  22. 22

    How to retrieve each line of a text file in order [python 3]

  23. 23

    Different Where Clause for each selected columns

  24. 24

    How can I prepend a line number and tab to each line of a text file?

  25. 25

    Different text file for each language

  26. 26

    How to read text file with uneven number of columns with python-pandas?

  27. 27

    Adding file path to each line of line of text

  28. 28

    Julia: How to import a graph from text file (csv with unequal number of 'columns')?

  29. 29

    Comparing each line in txt file to another text file in each line

HotTag

Archive