Solved

parse convert xml feed to text (python)

Posted on 2016-11-13
2
26 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
Comment Utility
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
Comment Utility
Fantastic   thanks
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

This article will show, step by step, how to integrate R code into a R Sweave document
This is an explanation of a simple data model to help parse a JSON feed
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
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.

762 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now