我正在尝试将大型.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其他传感器也有同样的想法。
我有一些代码可以成功地将文件转换为我需要的四个独立产品。然而;我想做的是使用公式值=值* 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文档但是我被卡住了。我发现的例子只显示使用文本字符串,而不是数值和字符串。 如果有人能指出我使用这样的科学数据的好教程,我将不胜感激。
提前感谢您的回答
答案 0 :(得分: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]) )
我还是编程的新手,有没有办法改进编码?