Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Python

Python and XML

 

 

Python python RSS feed   Index | Next | Previous | View Threaded


mat at frheaven

Aug 3, 2003, 10:34 AM

Post #1 of 11 (562 views)
Permalink
Python and XML

Hi!

I want to make an application that needs to parse an xml file, but I
always have weird errors, I've got Python 2.3

Here's the beginning of my prog:

from xml.dom import pulldom

and here's python's answer:

Traceback (most recent call last):
File "xml.py", line 1, in ?
from xml.dom import pulldom
File "C:\progs\xml.py", line 1, in ?
from xml.dom import pulldom
ImportError: No module named dom

When I look in my Python folder I see .py files in xml/dom...

Thanks for any help.

--
Matthieu M. <mat [at] frheaven>
http://www.frheaven.com


martin at v

Aug 3, 2003, 10:48 AM

Post #2 of 11 (531 views)
Permalink
Python and XML [In reply to]

"Matthieu M." <mat [at] frheaven> writes:

> from xml.dom import pulldom
[...]
> ImportError: No module named dom
>
> When I look in my Python folder I see .py files in xml/dom...

Please report

1. the output of

import xml
print xml.__file__

2. The contents of the directory printed above

3. The contents of the dom directory inside.

Regards,
Martin


mat at frheaven

Aug 3, 2003, 11:37 AM

Post #3 of 11 (527 views)
Permalink
Python and XML [In reply to]

Martin v. Löwis wrote:

> 1. the output of
>
> import xml
> print xml.__file__

c:\python23\lib\xml\__init__.pyc

> 2. The contents of the directory printed above

C:\Python23\Lib\xml

03/08/2003 15:48 <REP> .
03/08/2003 15:48 <REP> ..
03/08/2003 15:48 <REP> dom
03/08/2003 15:46 <REP> parsers
03/08/2003 15:48 <REP> sax
26/01/2003 10:06 1 053 __init__.py
03/08/2003 15:48 1 053 __init__.pyc

> 3. The contents of the dom directory inside.

C:\Python23\Lib\xml\dom

03/08/2003 15:48 <REP> .
03/08/2003 15:48 <REP> ..
24/04/2003 20:03 3 580 domreg.py
03/08/2003 15:48 3 894 domreg.pyc
29/06/2003 01:09 37 362 expatbuilder.py
25/01/2003 10:11 5 471 minicompat.py
03/08/2003 15:48 6 279 minicompat.pyc
06/04/2003 22:12 67 941 minidom.py
25/01/2003 12:07 964 NodeFilter.py
24/04/2003 20:03 12 325 pulldom.py
03/08/2003 15:48 16 975 pulldom.pyc
25/01/2003 10:29 12 772 xmlbuilder.py
27/02/2003 17:13 4 137 __init__.py
03/08/2003 15:48 8 642 __init__.pyc

In fact I've done all this things in the Python Shell, otherwise, when I
lauch it with "python foo.py" I've got the same kind of errors.
I think the problem comes from here, but I'm not skilled in Python, so...

Thanks

--
Matthieu M. <mat [at] frheaven>
http://www.frheaven.com


martin at v

Aug 3, 2003, 1:11 PM

Post #4 of 11 (530 views)
Permalink
Python and XML [In reply to]

"Matthieu M." <mat [at] frheaven> writes:

> In fact I've done all this things in the Python Shell, otherwise, when
> I lauch it with "python foo.py" I've got the same kind of errors.
> I think the problem comes from here, but I'm not skilled in Python, so...

Sorry, I have no idea why it fails to find the dom package.

Regards,
Martin


and-google at doxdesk

Aug 3, 2003, 3:14 PM

Post #5 of 11 (530 views)
Permalink
Python and XML [In reply to]

"Matthieu M." <mat [at] frheaven> wrote:

> from xml.dom import pulldom

> File "xml.py", line 1, in ?

You have a file called 'xml.py' somewhere on the pythonpath, which is
replacing the normal 'xml' package.

However if it doesn't interfere when you try to import xml from the
interpreter, likelihood is it's an xml.py file in the same directory
as the program's script file - check there.

For some reason, the current working directory is included in Python's
default module search path, before all other directories. If anyone can
explain why this is the Right Thing I'd be interested to know.

--
Andrew Clover
mailto:and [at] doxdesk
http://www.doxdesk.com/


missive at hotmail

Aug 3, 2003, 3:26 PM

Post #6 of 11 (525 views)
Permalink
Python and XML [In reply to]

In article <bgjh22$gun$1 [at] news-reader3>, Matthieu M. wrote:
> Hi!
>
> I want to make an application that needs to parse an xml file, but I
> always have weird errors, I've got Python 2.3
>
> Here's the beginning of my prog:
>
> from xml.dom import pulldom
>
> and here's python's answer:
>
> Traceback (most recent call last):
> File "xml.py", line 1, in ?
> from xml.dom import pulldom
> File "C:\progs\xml.py", line 1, in ?
> from xml.dom import pulldom
> ImportError: No module named dom
>
> When I look in my Python folder I see .py files in xml/dom...
>
> Thanks for any help.
>


I think the problem is that your program is called xml.py

so, when you say:

from xml import ....

it is trying to import from YOUR PROGRAM instead of the standard lib.
Try renaming your program.


mat at frheaven

Aug 4, 2003, 2:44 PM

Post #7 of 11 (532 views)
Permalink
Python and XML [In reply to]

Lee Harr wrote:

> I think the problem is that your program is called xml.py
>
> so, when you say:
>
> from xml import ....
>
> it is trying to import from YOUR PROGRAM instead of the standard lib.
> Try renaming your program.

you were right :)
Thanks a lot, I didn't know that python searched in the active directory
by default...

--
Matthieu M. <mat [at] frheaven>
http://www.frheaven.com


kveretennicov at gmail

Mar 29, 2008, 3:12 PM

Post #8 of 11 (526 views)
Permalink
Re: Python and xml [In reply to]

On Sat, Mar 29, 2008 at 3:57 PM, Doran, Harold <HDoran [at] air> wrote:

> I am a python neophyte who has used python to parse through text files
> using basic functions and no OOP experience. I have a need to process some
> xml files and from what I am now reading python is the exact tool I need to
> work through this issue.
>
> However, as I read online docs and peruse which books to buy, I am quickly
> becoming overwhelmed with the topic and could use some guidance on how to
> best tackle my task.
>
> You can start with this basic example (requires Python 2.5):

spam.xml:
<monty>
<episode number="14">Dinsdale (Face the Press)</episode>
<episode number="15">The Spanish Inquisition</episode>
<episode number="16">Deja vu</episode>
<episode number="17">The Buzz Aldrin Show</episode>
<episode number="18">Live From the Grill-O-Mat Snack Bar</episode>
<episode number="19">It's a Living</episode>
<episode number="20">The Attila the Hun Show</episode>
<episode number="21">Archaeology Today</episode>
<episode number="22">How to Recognize Different Parts of the
Body</episode>
<episode number="23">Scott of the Antarctic</episode>
<episode number="24">How Not to Be Seen</episode>
<episode number="25">Spam</episode>
<episode number="26">Royal Episode 13</episode>
</monty>

spam.py:
from xml.etree.ElementTree import ElementTree as ET

et = ET(file='spam.xml')
for episode in et.findall('episode'):
print episode.attrib['number'] + ':', '"' + episode.text + '"'


Use standard csv module if you want to produce csv ouput (
http://docs.python.org/lib/module-csv.html).

--
kv


stefan_ml at behnel

May 16, 2012, 7:23 AM

Post #9 of 11 (361 views)
Permalink
Re: python and xml [In reply to]

Nibin V M, 16.05.2012 16:16:
> I am trying to use cPanel XML-API and every API call return data in XML
> format. I would like to know how to manipulate the data here.
>
> For eg: How can I read the CPU load data from the below output
>
>
> <loadavg><one>0.00</one><five>0.00</five><fifteen>0.00</fifteen></loadavg><!--
> whostmgrd -->

Here's some untested code to print the text values:

import xml.etree.ElementTree as ET

loadavg = ET.fromstring('<loadavg><one>0.00</one><five>0.00</five>'
'<fifteen>0.00</fifteen></loadavg>')

for interval_tag in ('one', 'five', 'fifteen'):
print(loadavg.findtext(interval_tag))


Stefan

--
http://mail.python.org/mailman/listinfo/python-list


nibinvm at gmail

May 16, 2012, 7:30 AM

Post #10 of 11 (356 views)
Permalink
Re: python and xml [In reply to]

thank you Stefan. but the XML output is assigned to a variable; how to
process the variable with XML contents?

On Wed, May 16, 2012 at 7:53 PM, Stefan Behnel <stefan_ml [at] behnel> wrote:

> Nibin V M, 16.05.2012 16:16:
> > I am trying to use cPanel XML-API and every API call return data in XML
> > format. I would like to know how to manipulate the data here.
> >
> > For eg: How can I read the CPU load data from the below output
> >
> >
> >
> <loadavg><one>0.00</one><five>0.00</five><fifteen>0.00</fifteen></loadavg><!--
> > whostmgrd -->
>
> Here's some untested code to print the text values:
>
> import xml.etree.ElementTree as ET
>
> loadavg = ET.fromstring('<loadavg><one>0.00</one><five>0.00</five>'
> '<fifteen>0.00</fifteen></loadavg>')
>
> for interval_tag in ('one', 'five', 'fifteen'):
> print(loadavg.findtext(interval_tag))
>
>
> Stefan
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>



--
Regards....

Nibin.

http://TechsWare.in


stefan_ml at behnel

May 16, 2012, 7:36 AM

Post #11 of 11 (358 views)
Permalink
Re: python and xml [In reply to]

Hi,

please don't top-post (I fixed the citation order below).


Nibin V M, 16.05.2012 16:30:
> On Wed, May 16, 2012 at 7:53 PM, Stefan Behnel wrote:
>> Nibin V M, 16.05.2012 16:16:
>>> I am trying to use cPanel XML-API and every API call return data in XML
>>> format. I would like to know how to manipulate the data here.
>>>
>>> For eg: How can I read the CPU load data from the below output
>>>
>>>
>>>
>> <loadavg><one>0.00</one><five>0.00</five><fifteen>0.00</fifteen>
>> </loadavg><!-- whostmgrd -->
>>
>> Here's some untested code to print the text values:
>>
>> import xml.etree.ElementTree as ET
>>
>> loadavg = ET.fromstring('<loadavg><one>0.00</one><five>0.00</five>'
>> '<fifteen>0.00</fifteen></loadavg>')
>>
>> for interval_tag in ('one', 'five', 'fifteen'):
>> print(loadavg.findtext(interval_tag))
>
> thank you Stefan. but the XML output is assigned to a variable; how to
> process the variable with XML contents?

Not sure what you mean. Maybe something like this?

xml = '<loadavg><one>0.00</one><five>0.00</five>...</loadavg>'
loadavg = ET.fromstring(xml)

Stefan

--
http://mail.python.org/mailman/listinfo/python-list

Python python RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.