python删除⽂本中指定内容_python遍历⽂本并删除特定内容展开全部
按 指定⾏数 分页读取好处理:def iterpage(istream, pagesize):
buffer = []
for data in istream:
buffer.append(data)
if len(buffer)>=pagesize:
yield buffer
buffer = []
if buffer:
yield buffer
with open("", 'rt') as handle:
for page in iterpage(handle, 1000):
print page # or your business logical
print "-"*32 # page break
删除⽂本⽂件62616964757a686964616fe59b9ee7ad9431333337613138的前N⾏:def removehead(filename, headlines):
buffer = []
with open(filename, 'rt') as handle:
for i, ln in enumerate(handle):
if ln < headlines:
continue
buffer.append(ln)
writelines在python中的用法with open(filename, 'wt') as handle:
handle.writelines(buffer)
或者:def getandremovehead(filename, headlines):
with open(filename, 'rt') as handle:
buffer = adlines()
with open(filename, 'wt') as handle:
handle.writelines(buffer[headlines:])
return buffer[:headlines]
但遇到⼤⽂本⽂件时,删除其中N⾏不是很理想的业务⽅案
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论