Klamsi
Klamsi

Reputation: 73

Search for unknown numbers in a txt and plot them

Recently, I started to evaluate some data with Python. However, it seems complicated to evaluate and manipulate my recorded data.

For instance, my .txt file consists of:

1551356567 0598523403 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0    
1551356567 0598523436 0000003362 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0    
1551356567 0598523469 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0    
1551356567 0598523502 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0    
1551356567 0598523535 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0      
1551356567 0598523766 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0    
1551356567 0598523799 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0    
1551356567 0598523832 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0    
1551356567 0598523865 0000003314 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0    
1551356567 0598523898 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0    
1551356567 0598523931 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0   
1551356568 0598524756 0000003384 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0  

The important values are only the third column (with 3362) and the first one (1551...), whereby the third column should be the x axis and the first the y axis. Only the lines with a value not equal to 0 are important. The idea is to create a loop which searches for values in the third column, and if there is a value != 0, then this value should be saved in a x-list (x) and the corresponding y value in a y-list (y).

Currently my script to read and manipulate the data looks like this:

import numpy as np

rawdata = np.loadtxt("file.txt")
num_lines = sum(1 for line in open("file.txt"))

with open("file.txt") as hv:  
   line = hv.readline()

x = list()
y = list()

i = 1
j = 0
while line != num_lines:
    if rawdata[j][2] != 0:
        x = x.append(rawdata[j][2])
        y = x.append(rawdata[j][0])
    else:
        j += 1
    if i == num_lines:
        break
    i += 1

print(x)
print(y)

I think there are some local and global variable problems but I couldn't solve them to lets say "update" my lists with the new values. At the end there should be a list with only:

[3362, 3314, 3384] for x and

[1551356567, 1551356567, 1551356568] for y

Do you have any suggestions how I can "update" my list?

Upvotes: 0

Views: 28

Answers (1)

Mark Tolonen
Mark Tolonen

Reputation: 177735

As you read each line, split it on whitespace and convert each column to integers:

x = []
y = []

with open('file.txt') as f:
    for line in f:
        data = [int(col) for col in line.split()]
        if data[2] != 0:
            x.append(data[2])
            y.append(data[0])

print(x)
print(y)

Output:

[3362, 3314, 3384]
[1551356567, 1551356567, 1551356568]

Upvotes: 2

Related Questions