473,397 Members | 1,960 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Need Python-XML help

Hello,

I'm stuck writing a piece of software. I'm using Python 2.5 on WinXP Pro. I don't know how to get done what I need to do. I have some standardized input that looks like this:

Expand|Select|Wrap|Line Numbers
  1. <somename>
  2. <modulename>
  3. <item>
  4. <id>#UNIQUE_ID#</id>
  5. <datetime>yyyy-MM-dd hh:mm:ss</datetime>
  6. <title>#TEXT TITLE#</title>
  7. <description>#TEXT BODY#</description>
  8. <link>#LINK TO ARTICLE#</link>
  9. </item>
  10. </modulename>
  11. </somename>
This is the output of a parserprogram I'm working with. What I want to do is write these <items> into seperate files, where the filenames is the #UNIQUE ID#. I want to write these little files into a map with the date in it. So for a date that is 22/03/07 I want the item-files in 220307\<item>.xml .
I've got no clue how to do this. I can copy the items to a new XML tree using ElementTree, but can't figure out how to extract the elements for the XML and use it in file/map naming. Could anybody give me some pointers?
Mar 26 '07 #1
1 1137
ghostdog74
511 Expert 256MB
you can use XML parsers that makes things easier for you,but I use regexp parsing ( or simple string manipulations) for this case.
As i don't understand the rest of the question, this little piece of code just get out the necessary information that you need between the "item" tag.
Expand|Select|Wrap|Line Numbers
  1. data = open("file").read()
  2. pattern = re.compile("<item>(.*?)</item>",re.M|re.DOTALL)
  3. for items in pattern.findall(data):
  4.     filename = re.findall("<id>(.*?)</id>",items)[0]
  5.     datetiming = re.findall("<datetime>(.*?)</datetime>",items)[0]
  6.     title = re.findall("<title>(.*?)</title>",items)[0]
  7.     desc = re.findall("<description>(.*?)</description>",items)[0]
  8.     link = re.findall("<link>(.*?)</link>",items)[0]
  9. print filename
  10. print datetiming
  11. print title
  12. print desc
  13. print link
  14.  
Mar 27 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Graeme Matthew | last post by:
Hi all, I just wanted to tell someone :-) I was previously a perl programmer and have been a visual basic frustrated developer for a number of years, only for the reason that corporates are so...
3
by: Graham Nicholls | last post by:
Hi, I'm trying to size a jpeg file. The file size is held in a short (2 byte integer) at a certain offset. Once I've found these two bytes (they're in MSB,LSB order), I need to convert them to...
21
by: Chris Reedy | last post by:
For everyone - Apologies for the length of this message. If you don't want to look at the long example, you can skip to the end of the message. And for the Python gurus among you, if you can...
9
by: Roy Smith | last post by:
I'm working on a prototype of a new application in Python. At some point, if this ever turns into a product, the powers that be will almost certainly demand that it be done in Perl. My job will...
10
by: Jeff Wagner | last post by:
I am in the process of learning Python (obsessively so). I've been through a few tutorials and read a Python book that was lent to me. I am now trying to put what I've learned to use by rewriting...
2
by: Aaron | last post by:
I have a data sructure setup and I populate it in a loop like so: y=0 while X: DS.name = "ASDF" DS.ID = 1234 list = DS; y = y + 1
10
by: blah | last post by:
i m currently in a network (LAN). i started python because i heard that it has great ability for networking programs and/or scripts, but i m losing my motivation with python because there are sooo...
3
by: PL | last post by:
I want to pass a 2D array from Python to C++, manipulate it in C++ (for example, add 1 to each element) and pass it back to Python. With these building blocks I will be able to figure out all the...
3
by: skip | last post by:
I'm having no success building the curses module on Solaris 8 (yes, I know it's ancient - advancing the state-of-the-art is not yet an option) for Python 2.4. Sun provides an apparently ancient...
14
by: mistral | last post by:
Need compile python code, source is in html and starts with parameters: #!/bin/sh - "exec" "python" "-O" "$0" "$@" I have installed ActivePython for windows.
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.