[Tutor] XML Programs
leam hall
leamhall at gmail.com
Mon Apr 16 09:13:17 EDT 2018
Yeah, understood.
Okay, knowing that others are smarter about python, and ElementTree,
here's some code I was using to parse XML. Took a while to recover
from. :)
Leam
#####
import xml.etree.ElementTree as ET
import os
import argparse
import fnmatch
def show_info(file, element):
action = ""
net_proto = ""
trans_proto = ""
r_port = ""
l_port = ""
direction = ""
name = ""
has_info = False
f_name = ""
id = element.attrib['name']
f_name = os.path.splitext(file)[0]
for setting in element.iter('Setting'):
if setting.attrib['name'] == 'Action':
action = setting.attrib['value']
has_info = True
elif setting.attrib['name'] == '+NetworkProtocol#0':
net_proto = setting.attrib['value']
has_info = True
elif setting.attrib['name'] == '+TransportProtocol#0':
trans_proto = setting.attrib['value']
has_info = True
elif setting.attrib['name'] == '+RemotePort#0':
r_port = setting.attrib['value']
has_info = True
elif setting.attrib['name'] == '+LocalPort#0':
l_port = setting.attrib['value']
has_info = True
elif setting.attrib['name'] == 'Direction':
direction = setting.attrib['value']
has_info = True
elif setting.attrib['name'] == 'Name':
name = setting.attrib['value']
has_info = True
if has_info:
outfile.write("%s ; %s ; %s ; %s ; %s ; %s ; %s ; %s ; %s\n" %
(f_name, id, name, action, net_proto, trans_proto, l_port, r_port, direction))
## Main
parser = argparse.ArgumentParser()
parser.add_argument("-o", "--outfile", default = "new_out.csv",
help="File to write to.")
parser.add_argument("-d", "--dir", default = ".", help="Directory of
the XML files.")
args = parser.parse_args()
indir = args.dir
outfile = open(args.outfile, 'w')
outfile.write("File ;Rule ID ;Name ;Action ; Network Protocol;
Transport Protocol; Local Port; Remote Port; Direction\n")
for file in os.listdir(indir):
if fnmatch.fnmatch(file, '*.xml'):
full_file = indir + "\\" + file
tree = ET.parse(full_file)
root = tree.getroot()
for element in root.iter('PolicySettings'):
show_info(file, element)
outfile.close()
####
On Mon, Apr 16, 2018 at 9:07 AM, Glen <glenuk at gmail.com> wrote:
> I understand, I'd love to use something else but the save game files are in
> XML so I have no choice :'(
>
> On 16 April 2018 at 13:54, leam hall <leamhall at gmail.com> wrote:
>>
>> On Mon, Apr 16, 2018 at 7:10 AM, Glen <glenuk at gmail.com> wrote:
>> > Hey guys,
>> >
>> > I'm writing a save-game editor for a game I play (just a project to
>> > learn).
>> > But I am struggling on how to structure the code, how to store the xml
>> > data
>> > in data structure etc,
>> >
>> > Can anyone recommend some source I can review that reads and writes data
>> > from an xml file.
>>
>> A friend's comment was "life is too short for XML". I like that. Have
>> you considered JSON? Taking it a step further, MongoDB (JSON) or
>> SQLite (SQL)? Both are pretty common and standard.
>>
>> While Python has its own stuff, like Pickle, that means you can only
>> use Python. Using something like JSON or SQL means others can use the
>> data and you get a chace to develop in a shared environment. :)
>> _______________________________________________
>> Tutor maillist - Tutor at python.org
>> To unsubscribe or change subscription options:
>> https://mail.python.org/mailman/listinfo/tutor
>
>
More information about the Tutor
mailing list