0

I have a xml file, in that file I have a tag named as SN and it occurs in multiple places.How do I write these line numbers as a list to another file.

1 Answers1

1

So, you can use sourceline!Have a look at the example I have given below. Assuming you have a SN tag in xml file at multiple places.

    from lxml import etree
    context = etree.terparse(your_file_path)
    line_number=[]
    for action,elem in context:
        if elem.tag=="SN" :
            print(elem.text)
            a=elem.sourceline
            print(a)
            line_number.append(a)

So here you have a empty list as line_number and when you go through the for loop fetching SN you can get its line number by sourceline and then append it to the list.

Chetan_Vasudevan
  • 2,414
  • 1
  • 13
  • 34