2021年1月23日星期六

How to loop over a file and delete parts of the file in Python?

I have a data structure such as following.

<?xml version='1.0' encoding='UTF-8'?>  <corpus name="corpus">    <recording audio="audio.wav" name="first audio">      <segment name="1" start="0" end="2">          <orth>some text 1</orth>      </segment>      <segment name="2" start="2" end="4">          <orth>some text 2</orth>      </segment>      <segment name="3" start="4" end="6">          <orth>some text 3</orth>      </segment>    </recording>  </corpus>  

given an input file containing number of files such as

1  3  

it would remove the segments that has those name. For example, 1 and 3 was given so segments with names 1 and 3 has been removed.

<?xml version='1.0' encoding='UTF-8'?>  <corpus name="corpus">    <recording audio="audio.wav" name="first audio">      <segment name="2" start="2" end="4">          <orth>some text 2</orth>      </segment>    </recording>  </corpus>  

the code I have so far

with open('file.txt', 'r') as inputFile:      w_file = inputFile.readlines()    w_file = w_file.strip('\n')    with open('to_delete_nums.txt', 'r') as File:      d_file = deleteFile.readlines()    d_file = d_file.strip('\n')    for line in w_file:      if line.contains("<segment name"):          for d in d_file:              //if segment name is equal to d then delete that segment.    

How do I accomplish this? I also think having 2 might be unnecessary is that correct?

https://stackoverflow.com/questions/65867111/how-to-loop-over-a-file-and-delete-parts-of-the-file-in-python January 24, 2021 at 12:21PM

没有评论:

发表评论