-2

我正在尝试自动将大型 .txt 文件转换为 .csv 以导入专用软件。我拥有的是几十个 .txt 文件,其中包含带有逗号分隔传感器数据的非常大的字符串:

Date,Time,[Mainline] Name,MAG4 Altitude Value,IL MAG1 Easting,IL MAG1 Northing,Magnetic Field MAG1 [uT] Value,Magnetic Field MAG1 [uT] Quality,MAG1 Depth Value,IL MAG2 Easting,IL MAG2 Northing,Magnetic Field MAG2 [uT] Value,Magnetic Field MAG2 [uT] Quality,MAG2 Depth Value,IL MAG3 Easting,IL MAG3 Northing,Magnetic Field MAG3 [uT] Value,Magnetic Field MAG3 [uT] Quality,MAG3 Depth Value,IL MAG4 Easting,IL MAG4 Northing,Magnetic Field MAG4 [uT] Value,Magnetic Field MAG4 [uT] Quality,MAG4 Depth Value,IL MRU Pitch,IL MRU Roll
25-9-2013,27:48.6,INF_01,464,579618.04,5807421.24,49008.96,2212,18.68,579616.64,5807420.71,49003.14,1284,18.73,579615.23,5807420.18,49005.78,1428,18.97,579613.83,5807419.65,48999.99,1382,18.99,4.68,-0.14
25-9-2013,27:49.1,INF_01,461,579618.35,5807420.4,49008.99,2197,18.68,579616.95,5807419.88,49003.01,1270,18.73,579615.54,5807419.35,49005.73,1416,18.97,579614.14,5807418.83,49000.01,1379,18.83,3.66,0.23
25-9-2013,27:49.3,INF_01,461,579618.49,5807420.07,49008.9,2192,18.68,579617.08,5807419.55,49003.13,1255,18.73,579615.68,5807419.02,49005.6,1416,18.97,579614.27,5807418.5,48999.73,1377,18.99,3.14,0.31

我需要将其转换为带有标题的 .csv 文件,并将每个传感器的信息分开。对于第一个传感器(mag1),我需要第 0、1、2、5 到 8 列,然后是第 3 列,最后是第 24 和 25 列。对于第二个传感器,第 0、1、2、9 到 13 列,然后是第 3 列、第 24 列和第 25 列. 其他传感器的想法相同。

我有一些代码可以成功地将文件转换为我需要的四个单独的产品。然而; 我想做的是使用公式 value = value*0.0101817 - 1.0283 转换第 3 列中的数据。

代码:

import csv,glob,os

list_of_files = glob.glob('C:/test/*.txt')

for filename in list_of_files:
    short_filename, extension = os.path.splitext(filename)
    file_out_mag1 = short_filename + '_mag1' + ".csv"
    file_out_mag2 = short_filename + '_mag2' + ".csv"
    file_out_mag3 = short_filename + '_mag3' + ".csv"
    file_out_mag4 = short_filename + '_mag4' + ".csv"

with open(filename,"r") as source:
    rdr= csv.reader( source )
    with open(file_out_mag1,"w") as result:
        wtr= csv.writer( result,lineterminator='\n' )
        for r in rdr:
            #r.strip() ->does not work, list has no attribute strip
            #r.split(',') -> does not work, list has no attribute split
            if r ==3:   #does not work?
                r = r*0.0101817 - 1.0283
            wtr.writerow( (r[0],r[1],r[2],r[4],r[5],r[6],r[7],r[8],r[3], r[24], r[25]) )

with open(filename,"r") as source:
    rdr= csv.reader( source )  #this line had to be added again        
    with open(file_out_mag2,"w") as result: #does not work, file empty, solved see above
        wtr= csv.writer( result,lineterminator='\n' )
        for r in rdr:
            wtr.writerow((r[0],r[1],r[2],r[9],r[10],r[11],r[12],r[13],r[3], r[24], r[25]) )


with open(filename,"r") as source:
    rdr= csv.reader( source )          
    with open(file_out_mag3,"w") as result: 
        wtr= csv.writer( result,lineterminator='\n' )
            for r in rdr:
            wtr.writerow((r[0],r[1],r[2],r[3],r[14],r[15],r[16],r[17],r[18],r[3], r[24], r[25]) )

with open(filename,"r") as source:
    rdr= csv.reader( source )          
    with open(file_out_mag4,"w") as result: 
        wtr= csv.writer( result,lineterminator='\n' )
        for r in rdr:
            wtr.writerow((r[0],r[1],r[2],r[19],r[20],r[21],r[22],r[23],r[3], r[24], r[25]) ) 

数学公式在我的程序中不起作用。如何将它添加到我的代码中,为什么它在这里不起作用?我假设这是因为它是错误的类型,但我不知道如何在不遇到有关列表类型等的一系列错误的情况下更改它。

我浏览了 stackoverflow 和 python 文档,但我被卡住了。我发现的示例仅显示使用文本字符串,而不是数值和字符串。如果有人可以为我指出一个处理此类科学数据的好教程,我将不胜感激。

提前感谢您的回答

4

1 回答 1

0

感谢 thegrinner 的建议,我解决了这个问题:

with open(filename,"r") as source:
rdr= csv.reader( source )
with open(file_out_mag1,"w") as result:
    wtr= csv.writer( result,lineterminator='\n' )
    for r in rdr:

        if r[3] != 'MAG4 Altitude Value':
            r[3] = float(r[3])*0.0101817 - 1.0283
        wtr.writerow( (r[0],r[1],r[2],r[4],r[5],r[6],r[7],r[8],r[3], r[24], r[25]) )

我还是编程新手,有没有办法改进编码?

于 2013-10-18T13:20:15.540 回答