Home > Blockchain >  Make loop temp variable correlate with index instead of string
Make loop temp variable correlate with index instead of string

Time:12-17

So I have this program that reads lines from a file and inserts them into a list line by line (not pictured in code.) My objective is to find a specific start index indicated by a number surrounded by XML formats and find a specific end index indicated by a "/Invoice". I am able to successfully find these indexes using the start_indexes and end_indexes functions I created below.

I was informed (and experienced firsthand) the dangers of del list in loops otherwise that solution would have been perfect. Instead, I was advised to add everything I wanted to delete to a new list that I would then somehow use to delete the difference from my original list.

With that context being given, my question is as follows: What is the best way to accomplish what I am trying to do with the def deletion_list()?

I am aware that the "lines" in lst_file are strings, and I am attempting to compare them to indexes. That's where I am stumped; I don't know a way to convert the temp variable that is a string and make it into an index so the function works as I expect, or if there is a better way to do it.

start_indexes = []
for i in str_lst:
    invoice_index_start = lst_file.index('<InvoiceNumber>'   i   '</InvoiceNumber>\n')
    start_indexes.append(invoice_index_start)

end_indexes = []
constant = '</Invoice>\n'
for i in range(0,len(start_indexes)):
    invoice_index_end = lst_file.index(constant, start_indexes[i])
    end_indexes.append(invoice_index_end   1)

result = []
def deletion_list():
    for lines in lst_file:
    if lst_file[] > lst_file[invoice_index_start] and lst_file[] < lst_file[invoice_index_end]
    result.append(lines)
    return lst_file

CodePudding user response:

If you want to delete items from a list, best way is to loop through a copy of original list and you can delete from the original list.

a: list = [1,2,3,4,5,6]

for item in a[:]:
    if item % 2 == 0:
        a.remove(item)

You can simplify your problem by using XML parsing. Refer this: XML parser-Real python

CodePudding user response:

I assume your list looks like similar as: Invoice_1.xml and you would remove InvoiceNumber 2 and 4.

Input:

<?xml version="1.0" encoding="utf-8"?>
<root>
  <Invoices>
    <InvoiceNumber>1</InvoiceNumber>
    <InvoiceNumber>2</InvoiceNumber>
    <InvoiceNumber>3</InvoiceNumber>
    <InvoiceNumber>4</InvoiceNumber>
  </Invoices>
</root>

You can parse the input XML file and write the changed XML to Invoice_2.xml:

import xml.etree.ElementTree as ET

tree = ET.parse('Invoice_1.xml')
root = tree.getroot()       
print("Original file:")
ET.dump(root)


rem_list = ['2', '4']
parent_map = {(c, p) for p in root.iter( ) for c in p}
for (c, p) in parent_map:
    if c.text in rem_list:
        p.remove(c)

tree.write('Invoice_2.xml', encoding='utf-8', xml_declaration=True)


tree1 = ET.parse('Invoice_2.xml')
root1 = tree1.getroot()
print("Changed file:")
ET.dump(root1)

Output:

<?xml version='1.0' encoding='utf-8'?>
<root>
  <Invoices>
    <InvoiceNumber>1</InvoiceNumber>
    <InvoiceNumber>3</InvoiceNumber>
  </Invoices>
</root>


    
  • Related