我有几个大的文本文本文件都具有相同的结构,我想删除前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”而“Best Unit”应该更改为“Best_Unit”.然后我可以使用我的其他脚本对数据进行地理编码.
我不能使用像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