简体   繁体   English

难以从文本文件读取数据并将其转换为浮点数

[英]Difficulty reading data from a text file and converting to a float

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 将我的开场白更改为“ open(os.path.join(root,'Report.TXT'),'r', encoding ='utf-16' )”解决了我的问题

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. 该程序递归地在文件夹中移动,找到一个名为Report.TXT的文件, Report.TXT一些数字,然后将它们正确地组织在excel文件中。 There's a lot of irrelevant information from this file, so I need to grab only a specific part of it (eg line 56, characters 72-95). 该文件中有很多不相关的信息,因此我只需要抓取文件的特定部分(例如,第56行,字符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): 这是这些Report.TXT文件之一的一部分示例,其中包含我要获取的信息(在ng / uL列下):

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. 这只是Report.TXT的一部分,实际的“ Compound1”在实际文件的第54行上。

I've managed to form something that will grab these and insert it into an excel file as a string: 我设法形成了一些东西,可以抓住这些东西并将其作为字符串插入到excel文件中:

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. 但是,如果将str(line[75:94].strip())替换为float(line[75:94].strip()) ,则会收到cannot convert string to float错误。 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? 关于如何解决此问题的任何想法?

converting to float is not such a good idea in this case. 在这种情况下,转换为float并不是一个好主意。 since you are copying it to a delimited file, it doesnt matter if you convert to float or not. 由于您将其复制到定界文件中,因此无论是否转换为float都没有关系。 more over(the floating point issue in python suggest not to convert to float using the standard library float() method. 更多内容(python中的浮点问题建议不要使用标准库float()方法转换为float。

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. 如有必要,请使用numpy将复数转换为十进制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM