I want to plot some data in a polar plot and the code I wrote works great if I import the data from a simple txt file with any headers or text in it. The problem is that I get the data from an automatic device in a format as follow:

Hello word      
Hi Again        
Angle [deg] Level of radiation
-180    -1
-175    -8.17
-170    -15
-165    -13.67

At the moment, I import the data with the following code, but if there is text or headers doesn't work:

for line in open(Data.txt, 'r'):
  values = [float(s) for s in line.split()]
  Position.append(values[0])
  Level.append(values[1])
  NormalizedLevel.append(values[2])

My goal is to have the first two rows stored as a text to be displayed somewhere in the plot, and then the following three columns stored in three different arrays. If possible, the name of each array should be the header of the column, but if it is not possible it is not a big issue!

Any ideas? Thanks in advance!

2 Answers

1
Valentino On Best Solutions

Skip the headers. If you know how many lines you have in the headers, you can do:

with open('Data.txt', 'r') as ff:
    lines = ff.readlines()
    for ll in lines[3:] #if you have 3 lines of header as in your example
        values = [float(s) for s in ll.split()]
        Position.append(values[0])
        Level.append(values[1])
        NormalizedLevel.append(values[2])

You can grab the header from (in this case) lines[:3] to later use. It's a list.

EDIT

In case you do not know the number of header's lines, you could use the following code:

header = []
with open('data.txt', 'r') as ff:
    lines = ff.readlines()

    for ll in lines:
        try:
            values = [float(s) for s in ll.split()]
            Position.append(values[0])
            Level.append(values[1])
            NormalizedLevel.append(values[2])
        except ValueError:
            header.append(ll)

Basically you try to split the string and to convert the entries of the list to floats. If something goes wrong, a ValueError exception is raised. The code assumes that line it's a header line and store it in a proper list.
Since you are dealing with a variable format, I think it's the best you can do.

1
benmccallum On

With a variable number of header lines, you'd need to first calculate numberOfHeaderLines you have. Once you know that, then you could use array slicing to pull off the rest of the data.

with open('Data.txt', 'r') as file:
    lines = file.readlines()

    numberOfHeaderLines = 0
    for line in lines
        values = lines.split()

        # perhaps store your header data somewhere

        if (values[0].isdigit())
            break; # exit this loop now we know we're at a data row
        numberOfHeaderLines++ # increment

    for line in lines[numberOfHeaderLines:] 
        values = [float(s) for s in lines.split()]
        Position.append(values[0])
        Level.append(values[1])
        NormalizedLevel.append(values[2])

There's probably more concise ways to do this, but (1) I'm not a Python guy, and (2) if you're new to programming it's important to know the fundamental approaches like this, which are essentially language independent.