我有几个大的文本文件都有相同的结构,我想删除前3行,然后从第4行删除非法字符。我不想读取整个数据集,然后修改,因为每个文件超过100MB,记录超过400万条。Range 150.0dB -64.9dBm
Mobile unit 1 Base -17.19968 145.40369 999.8
Fixed unit 2 Mobile -17.20180 145.29514 533.0
Latitude Longitude Rx(dB) Best unit
-17.06694 145.23158 -050.5 2
-17.06695 145.23297 -044.1 2
因此,应删除第1、2和3行,在第4行中,“Rx(db)”应仅为“Rx”,并将“最佳单元”更改为“最佳单元”。然后我可以使用其他脚本对数据进行地理编码。
我不能使用像grep(as in this question)这样的命令行程序,因为前3行并不完全相同-每个文件中的数字(如150.0dB,-64*)都会更改,因此您只需删除整个第1-3行,然后grep或类似程序就可以在第4行执行搜索替换。
谢谢你们
===编辑新的pythonic方法来处理@heltonbiker中的较大文件。错误。import os, re
##infile = arcpy.GetParameter(0)
##chunk_size = arcpy.GetParameter(1) # number of records in each dataset
infile='trc_emerald.txt'
fc= open(infile)
Name = infile[:infile.rfind('.')]
outfile = Name+'_db.txt'
line4 = fc.readlines(100)[3]
line4 = re.sub('\([^\)].*?\)', '', line4)
line4 = re.sub('Best(\s.*?)', 'Best_', line4)
newfilestring = ''.join(line4 + [line for line in fc.readlines[4:]])
fc.close()
newfile = open(outfile, 'w')
newfile.write(newfilestring)
newfile.close()
del lines
del outfile
del Name
#return chunk_size, fl
#arcpy.SetParameterAsText(2, fl)
print "Completed"Traceback (most recent call last): File "P:\2012\Job_044_DM_Radio_Propogation\Working\FinalPropogation\TRC_Emerald\working\clean_file_1c.py",
line 13, in
newfilestring = ''.join(line4 + [line for line in fc.readlines[4:]]) TypeError: 'builtin_function_or_method' object is
unsubscriptable