0

I am still a beginner in Python. I am reading a tab delimited text file generated from a software to filter out a set of values. But the values generated are all in a format similar to IEEE floating point format. (for example : 1.5656565656E+02).

My tab delimited file is something like this(actually it is 250 columns. roughly it is like this)

Time [s]       Offset_Angle              observability_Analysis
0.00E+00      0.89040261167028E+00        0.00000000000000E+00
4.32E+02      0.21319658757004E+00        0.00000000000000E+00
8.64E+02      0.26803683992125E+00        0.00000000000000E+00
1.30E+03      2.67379011780784E+02        1.00000000000000E+00
1.73E+03      2.89704767087971E+02        1.00000000000000E+00
2.16E+03      2.93302157473406E+02        1.00000000000000E+00

Th aim is to filter those Offset_Angle values that have their observability_Analysis as 1.0000000000000E+00 But as of now I am trying to print the row number which has observability_Analysis as 1.0000000000000E+00.

The code i tried is like this

with open('E:\\trialpy\\textfile.txt') as f:
    reader = csv.reader(f, delimiter="\t")
    d = list(reader)

for ind in range(len(d)):
    if d[ind][2] == "1.00000000000000E+00":
        print ind

The first set of code works correctly. The print ind is just empty. It doesnt give any values. Please help me with this. And sorry if the question is very silly. Stuck at this place for sometime now. Is there a way to solve this or I have to switch to Pandas

Thanks.

4
  • This code works just fine on my machine (I get the output 4\n5\n6\n). Maybe you mixed up the index column in your 250-column file? Try printing the value of d[ind][2] before the if and check what actually is there. Commented Oct 28, 2015 at 10:37
  • If I try printing the values before the if statement it prints the values as it is in the file. Dont know where the problem is Commented Oct 28, 2015 at 11:09
  • Do you mean it prints all the values of the correct column? Or does it print more than that? Commented Oct 28, 2015 at 11:10
  • It printed all the values Commented Oct 28, 2015 at 13:50

1 Answer 1

1

The problem seems to be that csv reader cannot properly process this data, so each line is saved as a single string into a list (row)...

with open('data.csv', 'r') as f1:
    r = csv.reader(f1, delimiter='\t')
    next(r, None) # skip first line
    for row in r:
        thirdc = row[0].split()[2]
        if float(thirdc) > 0.0:
            print(thirdc)

    1.00000000000000E+00
    1.00000000000000E+00
    1.00000000000000E+00
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.