0

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

3
Contributors
3
Replies
4
Views
7 Years
Discussion Span
Last Post by rmsagar
0

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.

Edited by vegaseat: dict

0

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.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.