python处理文本文件 删除不要的行_在python中从大文本文件中删除特定行

我有几个大的文本文件都有相同的结构,我想删除前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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值