Mahesh m
Mahesh m

Reputation: 49

Converting a xml DOM structure form a file into a dictionary in python

i have a file , not actually an xml file , but a text file which looks something like this (example.txt)-

<01-01-20>
hello hello . . random content
</01-01-20>


<04-01-20>
hello again. . some more random content.....

</04-01-20>

I would like to store the values in the text file as key value pair in dictionary , something like:

{<01-01-20>:"hello hello. . ",<04-01-20>:"hello again.. . "}

Is this possible. Please guide me on how to do this in python

EDIT -

THE CODE I CAME UP WITH ,

import re
import mmap

tf1 = open('text1.txt', 'r+b')  ##first kosha
tf2 = open('text2.txt', 'r')  ##second kosha

first = []
second = []
reg = re.compile("^<.*>$")     ##sample regular expression for < >
for line in tf1:
    first += reg.findall(line)

for line in tf2:
    second += reg.findall(line)

print('Tags that are present in file 1 but not in file2')    
for i in first:
    if i not in second:
        print(i)




tf1.close()
tf2.close()

Now i need to compare the hyms from both text files and tell if they are alike or not , so i figured it'd be better to put it into a dictionary. Please help.

Upvotes: 0

Views: 98

Answers (1)

KittoMi
KittoMi

Reputation: 459

This is the full code you actually are expecting.

Code

with open("file_in.txt", "r") as file:
    dict1 = {}
    lines = file.readlines()
    for i in range(len(lines)):
        try:
            if lines[i].startswith('<') and lines[i+1] != '\n':
                dict1[lines[i].strip()] = lines[i+1].strip()
        except:
            print("File read complete!")
    
print(dict1)

Output

{'<01-01-20>': 'hello hello . . random content', '<04-01-20>': 'hello again. . some more random content.....'}

Upvotes: 1

Related Questions