Parsing Query

This is the place for queries that don't fit in any of the other categories.

Parsing Query

Postby zedex » Fri Aug 30, 2013 6:31 pm

Hi All,

I am Python newbie, currently i am writing simple script which will connect to database run simple query and format it to XML.

I am using pyodbc to connect and fetch the details, while my problem is not specific to python you can consider as general programing query. and for storing to xml i am suing lxml module.

Sample Query Result

Code: Select all
Col 1 = Col 2
Code/A/B = Dir
Code/A/B/a.txt = File
Code/A/B/b.txt = File
Code/A/B/c.txt = File
Code/A/B/d.txt = File
Code/A/B/E = Dir
Code/A/B/E/text = File
Code/A/B/E/text1 = File
Code/A/B/f.txt = File
Code/A/Else = Dir
Code/A/Else/e.txt = File


where Col2 == "DIR" it should form nested XML like shown below, and output should be something like below

Code: Select all
<Code>
    <A>
   <B>
            <file name="a.txt"/>
            <file name="b.txt"/>
            <file name="c.txt"/>
            <file name="d.txt"/>
       <E>
           <file name="text"/>
           <file name="text1"/>
       </E>
   </B>
    </A>
    <Else>
         <file name="e.txt" />
    </Else>
</Code>


Currently struggling @
1. Not sure how to store data , i initially thought Dict but as D[ code][A][B] = [List of files] but then i noticed there is [E] (1 more level) so i can not have it!
2. As query results are returned in ordered format, shown below

Code: Select all
Code/A/B/d.txt = File   < 1 entry
Code/A/B/E = Dir
Code/A/B/E/text = File
Code/A/B/E/text1 = File
Code/A/B/f.txt = File   < Next entry


I am not sure how would i move xml to append sub elements to <C>, then to <E> and then again add sub elements to <C>?
Last edited by stranac on Fri Aug 30, 2013 7:09 pm, edited 3 times in total.
Reason: Corrected some broken code tags
zedex
 
Posts: 2
Joined: Fri Aug 30, 2013 6:13 pm

Re: Parsing Query

Postby TheFunk » Fri Aug 30, 2013 7:04 pm

Forgive me if this isn't helpful, but the way I'd format data for XML is by concatenating strings based on certain conditions and then just writing the information to a file. Here's an excerpt with HTML I've been working on. It's probably reinventing the wheel...as a matter of fact I'm almost sure it is, but it works well.

Code: Select all
                        date = datetime.datetime.now().strftime('%b%y')                       
                        report = open( date + '-report.html','w')
                        report.write('<!DOCTYPE html>\n<html>\n<body>\n<h1>' + date + ' Report</h1>\n<p>Report Report Report Report</p>\n</body>\n</html>')
                        # etc etc etc
TheFunk
 
Posts: 27
Joined: Fri Aug 30, 2013 5:46 pm

Re: Parsing Query

Postby ochichinyezaboombwa » Fri Aug 30, 2013 7:56 pm

Not sure about XML (never understood why would anybody use it), but here's one way to turn your data into some meaningful Python data structure:
Code: Select all
lines = """Code/A/B = Dir
Code/A/B/a.txt = File
Code/A/B/b.txt = File
Code/A/B/c.txt = File
Code/A/B/d.txt = File
Code/A/B/E = Dir
Code/A/B/E/text = File
Code/A/B/E/text1 = File
Code/A/B/f.txt = File
Code/A/Else = Dir
Code/A/Else/e.txt = File
""".splitlines(False)

h = {}
for ln in lines:
    data, tp = [x.strip() for x in ln.split("=")]
    assert tp in ("Dir", "File")
    xxx = data.split("/")
    if tp == "Dir":
        key = tuple(xxx)
        val = []
        h[key] = val
    else:
        key = tuple(xxx[:-1])
        val = xxx[-1]
        h[key].append(val)

for k in h:
    print k, h[k]


Which prints:
Code: Select all
('Code', 'A', 'Else') ['e.txt']
('Code', 'A', 'B', 'E') ['text', 'text1']
('Code', 'A', 'B') ['a.txt', 'b.txt', 'c.txt', 'd.txt', 'f.txt']


Converting this to xml should be trivial; I recommend json instead.
ochichinyezaboombwa
 
Posts: 200
Joined: Tue Jun 04, 2013 7:53 pm

Re: Parsing Query

Postby zedex » Tue Sep 10, 2013 12:18 pm

Thanks ochichinyezaboombwa , this is exactly what i was looking for!

XML is one of many formats supported
zedex
 
Posts: 2
Joined: Fri Aug 30, 2013 6:13 pm


Return to General Coding Help

Who is online

Users browsing this forum: Bing [Bot], Google [Bot], snippsat, W3C [Linkcheck] and 4 guests