How to loop through data and make changes and save it to a file in Python?

  python, python-3.x

I have a pattern such as below. name after segments increases by one every single time. So it would be 1, 2, 3, 4…100, 101 and so on. I am trying to change this numbers and keep the increasing by 1 pattern.

<segment name="0" start="0.0" end="6.6">
<orth>something</orth>
</segment>
<segment name="1" start="6.6" end="10.2">
<orth>something</orth>
</segment>

the full dataset has this pattern but I am just trying to change the name after <segment

<?xml version='1.0' encoding='UTF-8'?>
<corpus name="corpus">
  <recording audio="audio01" name="name01">
    <segment name="0" start="2.98" end="3.67">
      <orth>sometext</orth>
</segment>
  </recording>
</corpus>

Here I try to set the starting number at 291199 and increase my way from there.

starting_num = 291199

with open('file.txt', 'r') as inputFile:
    w_file = inputFile.readlines()

w_file = w_file.strip('n')

for line in w_file:
    if line.contains("<segment name"):
        //set name to starting_num
        starting_num = starting_num + 1

How would I be able to accomplish this task?

Source: Python-3x Questions

LEAVE A COMMENT