Python read excel colum, search in xml and if found replace with value in excel second column

Viewed 13

I´m migrating a website from .net to wordpress. I need to merge 2 files so I can import all posts information to Wordpress.

I have 2 files:

a. XLSX file with 2 colums. A = Id of post and B URL to mp3 file. b. XML file with the posts information.

I´m trying to create a python code that reads a value from a XLSX file on column A and if found in a XML file, then replace it with the value of XLSX column B of the same row.

1- this code works if xml file is short, but the in the actual xml with 2 million lines fails, and some times its empty.

2 - Also when it works, the values get inside the the actual xml structure, but without order. Maybe if possible, I´ll like that when the value is found on the XML, go to the end of line and append the value with the tags in the next line.

I need a clean structure so I can import this XML to wordpress.

import pandas as pd


id = ()
link = ()
lines = []
df = pd.read_excel('C:/TEMP/PAnamaIds_prueba.xlsx')

for index, row in df.iterrows():       # Reads xlsx file row by row..
    id = str(row["iD"])                # Store read id..
    link = row["Url"]                  # Store read URL..
    replacement = ""

    with open("C:/TEMP/XML_RP_TEST_2.xml", "r", errors='ignore') as file:
        for line in file:
            line = line.strip()
            changes = line.replace(id, "<mp3>" + link + "</mp3>")
            replacement = replacement + changes + "\n"

        file.close()

        wr = open("C:/TEMP/XML_RP_TEST_2.xml", "w")
        wr.write(replacement)
        wr.close()
0 Answers
Related