In my file, I have lines similar to this
<Log LogID="633198520" LogLevel="Normal" Date="10/01/09 19:18" Type="Error" Source="Mentor" Text="Failed to process the request." />

When the line is read, if it is converted as field value pair I thought it will be easy to take required field. Please advise how this can be achieved.

-Thanks MS

Recommended Answers

All 3 Replies

If you only have one line than you can simply do this ...

line = '<Log LogID="633198520" LogLevel="Normal" Date="10/01/09 19:18" Type="Error" Source="Mentor" Text="Failed to process the request." />'

# slice off first 5 and last 3 characters 
line = line[5:-3]
# testing ...
print line

line = line.replace('" ', '"|')
# testing ...
print line

line_list = line.split('|')
# testing ...
print line_list

print('-'*60)

for item in line_list:
    exec(item)

# testing ...
print LogID
print Date
print Text

If you have multiple lines you have to invent a way to keep the variables separate for each line, may with an array assignment. I would do a list of dictionaries where the key would be the variable name.

Also note that the line looks like an xml tag. If your file is an xml file, you could parse it with lxml for example.

Thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.