Python Forum
Using xml.parsers.expat I can't get parser.ParseFile(f) to work
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Using xml.parsers.expat I can't get parser.ParseFile(f) to work
#3
(Apr-24-2024, 05:33 AM)Pedroski55 Wrote: What does xmlparser.ParseFile(f) want for f?

Is the module too old?
Still work with some changes over to Python 3,as mention bye Gribouillis need 'rb'
# coexpat.py
#
# An example of pushing XML events generated by the low-level expat
# XML library into coroutines.

import xml.parsers.expat

def expat_parse(f,target):
    parser = xml.parsers.expat.ParserCreate()
    parser.buffer_size = 65536
    parser.buffer_text = True
    # parser.returns_unicode = False
    parser.StartElementHandler = \
       lambda name,attrs: target.send(('start',(name,attrs)))
    parser.EndElementHandler = \
       lambda name: target.send(('end',name))
    parser.CharacterDataHandler = \
       lambda data: target.send(('text',data))
    parser.ParseFile(f)

# Example.  This uses the bus processing code from earlier with no changes.

if __name__ == '__main__':
    from buses import *

    with open("allroutes.xml", 'rb') as file:
        expat_parse(file,
            buses_to_dicts(
            filter_on_field("route", "22",
            filter_on_field("direction", "North Bound",
            bus_locations()))))
Output:
22,1485,"North Bound",41.880481123924255,-87.62948191165924 22,1629,"North Bound",42.01851969751819,-87.6730209876751 22,1489,"North Bound",41.962393500588156,-87.66610128229314 .....
Other changes in the two files this code calles,is just over to print() function.
Eg in coroutine.py line 21 print line, is in Python 3 print(line, end='').
Pedroski55 likes this post
Reply


Messages In This Thread
RE: Using xml.parsers.expat I can't get parser.ParseFile(f) to work - by snippsat - Apr-24-2024, 07:15 AM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020