Difficulty reading data from a text file and converting to a float

210 views Asked by At

UPDATE: My problem was due to the input file having an odd encoding. Changing my opening statement to "open(os.path.join(root, 'Report.TXT'), 'r', encoding='utf-16')" fixed my problem

ORIGINAL TEXT I'm trying to make a program that will allow me to more easily organize data from some lab equipment. This program recursively moves through folders, locates a file named Report.TXT, grabs a few numbers from it, and correctly organizes them in an excel file. There's a lot of irrelevant information from this file, so I need to grab only a specific part of it (e.g. line 56, characters 72-95).

Here's an example of a part of one of these Report.TXT files containing information I want to grab (under the ng/uL column):

RetTime  Type     Area     Amt/Area    Amount   Grp   Name
 [min]         [nRIU*s]               [ng/ul]  
-------|------|----------|----------|----------|--|------------------
  4.232 BB     6164.18262 1.13680e-5 7.00746e-1    Compound1                                        
  5.046 BV     2.73487e5  1.34197e-5   36.70109    Compound2                                           
  5.391 VB     3.10324e5  1.34678e-5   41.79371    Compound3                                            
  6.145            -          -          -         Compound4                                           
  7.258            -          -          -         Compound5                                          
  8.159            -          -          -         Compound6                                           
 11.092 BB     3447.12158 2.94609e-5    1.01555    Compound7                                           
Totals :                               80.21110

This is only a portion of the Report.TXT, the actual "Compound1" is on line 54 of the real file.

I've managed to form something that will grab these and insert it into an excel file as a string:

for rootdir in range(1,tdirs+1):
    flask = 0
    for root, subFolders, files in os.walk(str(rootdir)):
        if 'Report.TXT' in files:
            flask += 1
            with open(os.path.join(root, 'Report.TXT'), 'r') as fin:
                print(root)
                for x in range(0,67):
                    line = fin.readline()
                    if x == 54:
                        if "-" in line[75:94]:
                            compound1 = 0
                        else:
                            compound1 = str(line[75:94].strip())
                        print(compound1)
                        datasheet.write(int(rootdir)+2,int(flask),compound1)
                    if x == 56:
                        if "-" in line[75:94]:
                            compound2 = 0
                        else:
                            compound2 = str(line[75:94].strip())
                        print(compound2)
                        datasheet.write(int(tdirs)+int(rootdir)+6,int(flask),compound2)

However, if I replace the str(line[75:94].strip()) with a float(line[75:94].strip()), then I get a cannot convert string to float error. The printing was just for my own troubleshooting but isn't seeming to give me any extra information.

Any ideas on what I can do to fix this?

1

There are 1 answers

2
rajesh.kanakabandi On

converting to float is not such a good idea in this case. since you are copying it to a delimited file, it doesnt matter if you convert to float or not. more over(the floating point issue in python suggest not to convert to float using the standard library float() method.

you will be better of writing the sting values since you would want to have you lab results accurate.

use numpy to convert the complex numbers to decimal if it is necessary.