Solved

parse convert xml feed to text (python)

Posted on 2016-11-13
2
81 Views
Last Modified: 2016-11-15
hi i am new to python and need to produce a text file based on the categories in  http://m.highways.gov.uk/feeds/rss/AllEvents/East%20Midlands.xml

I have read loads and all fail. so back to basics...

ideally i need <category> , <title> , <road> ,<eventEnd>


code so far and failing

#!/usr/bin/python2.6
# Copyright: robert@roberteklund.org 2012
# East Midlands.xml


from xml.etree import ElementTree

with open('East Midlands.xml', 'rt') as f:
    tree = ElementTree.parse(f)

for node in tree.iter():
    title = node.attrib.get('title')
    category = node.findtext('category')
print '  %s' % (title)
    

Open in new window

0
Comment
Question by:James Murrell
2 Comments
 
LVL 19

Accepted Solution

by:
Kim Ryan earned 500 total points
ID: 41885835
There is a package just for parsing RSS feeds, which is much simpler to use (RSS is afrom of XML). This code worked for me:

import feedparser

d = feedparser.parse('http://m.highways.gov.uk/feeds/rss/AllEvents/East%20Midlands.xml')
 
for post in d.entries:
    print(post.title + ' :' + post.category + ' : ' +  post.road) 

Open in new window


Output:
M1 southbound between J22 and J21A | Southbound | Road Works :Road Works :M1
link road from M1 J23A to A453 northbound | Northbound | Road Works :Road Works :M1
link road from M1 J23A to A453 northbound | Northbound | Road Works :Road Works :M1
A42 northbound between A453 and M1/A453 | Northbound | Road Works :Road Works :A42
...


You may need to install the feedparser package to your system, either using pip or download it here https://pypi.python.org/pypi/feedparser

I'm using Python 3 so print statement requires brackets. If you are on version 2, then just remove the brackets.
0
 
LVL 31

Author Closing Comment

by:James Murrell
ID: 41888498
Fantastic   thanks
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
AWS - HAProxy- KeepAlived 5 42
How to concatenate fields in zpl, and how to use conditional statement? 3 16
maybe no no httpd.conf 6 47
awk to variable in bash 2 42
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
Join Greg Farro and Ethan Banks from Packet Pushers (http://packetpushers.net/podcast/podcasts/pq-show-93-smart-network-monitoring-paessler-sponsored/) and Greg Ross from Paessler (https://www.paessler.com/prtg) for a discussion about smart network …
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question