python中如何在写文件之前删除文件内容_从文件python中删除字符串和字符串之前的所有行...

1586010002-jmsa.png

I have a filename with thousands of lines of data in it.

I am reading in the filename and editing it.

The following tag is about ~900 lines in or more (it varies per file):

I need to remove that line and everything before it in several files.

so I need to the code to search for that tag and delete it and everything above it

it will not always be 900 lines down, it will vary; however, the tag will always be the same.

I already have the code to read in the lines and write to a file. I just need the logic behind finding that line and removing it and everything before it.

I tried reading the file in line by line and then writing to a new file once it hits on that string, but the logic is incorrect:

readFile = open(firstFile)

lines = readFile.readlines()

readFile.close()

w = open('test','w')

for item in lines:

if (item == ""):

w.writelines(item)

w.close()

In addition, the exact string will not be the same in each file. The value "test" will be different. I perhaps need to check for the tag name ""

Any help will be much appreciated.

解决方案

You can use a flag like tag_found to check when lines should be written to the output. You initially set the flag to False, and then change it to True once you've found the right tag. When the flag is True, you copy the line to the output file.

TAG = ''

tag_found = False

with open('tag_input.txt') as in_file:

with open('tag_output.txt', 'w') as out_file:

for line in in_file:

if not tag_found:

if line.strip() == TAG:

tag_found = True

else:

out_file.write(line)

PS: The with open(filename) as in_file: syntax is using what Python calls a "context manager"- see here for an overview. The short explanation of them is that they automatically take care of closing the file safely for you when the with: block is finished, so you don't have to remember to put in my_file.close() statements.

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值