Using xml.parsers.expat I can't get parser.ParseFile(f) to work - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Using xml.parsers.expat I can't get parser.ParseFile(f) to work (/thread-42023.html) |
Using xml.parsers.expat I can't get parser.ParseFile(f) to work - Pedroski55 - Apr-24-2024 This code comes from Dave Beazly and it is a bit old, Python 2.something. I am trying to get it to work in Python 3. This is from Part 3 : Coroutines and Event Dispatching, coexpat.py Up to now, everything worked. I have this function and various others, all available on the link above. 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)When I enter as per the given code: expat_parse(open(xmlfile), buses_to_dicts(filter_on_field("route","22", filter_on_field("direction","North Bound", bus_locations()))))I get this error: Quote:Traceback (most recent call last): But read() should return a string, not bytes, I believe. The docs say: Quote:xmlparser.ParseFile(file) If I do this there is no problem: Quote:data = open(xmlfile) So now I don't know what the problem is, or what file in xmlparser.ParseFile(file) should be!I thought maybe the xml file is corrupt, but I can open it and .read() it to a string (13817 lines) and it displays in my browser ok. What does xmlparser.ParseFile(f) want for f? Is the module too old? RE: Using xml.parsers.expat I can't get parser.ParseFile(f) to work - Gribouillis - Apr-24-2024 Have you tried opening in binary mode? open(xmlfile, 'rb') RE: Using xml.parsers.expat I can't get parser.ParseFile(f) to work - snippsat - Apr-24-2024 (Apr-24-2024, 05:33 AM)Pedroski55 Wrote: What does xmlparser.ParseFile(f) want for f?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())))) 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='') .
RE: Using xml.parsers.expat I can't get parser.ParseFile(f) to work - Pedroski55 - Apr-24-2024 Thanks! Worked first time with 'rb'! |