473,409 Members | 2,057 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,409 software developers and data experts.

XML/encoding/prolog/python hell...

I am a beginning pythoner and I am having a terrible time trying to
figure out how to do something that (it would seeme to me) should be
fairly simple.

I have a CSV file of unknown encoding and I need to parse that file to
get the fields <--- DONE
I need to create an xml document that has the proper prolog and
namespace information in it. <--- NOT DONE
I need it to be encoded properly<--- Looks right in IE, not right in
any other app.

I should say that I have googled my butt off, tried ElementTree,
CSV2XML, and various other things and cannot get any of them to work.

A sample of the output I am looking for is as follows:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns2:boxes xmlns:ns2="Boxes">
<ns2:box id="9" mac="333333" d_a="2006" hw_ver="v1.1" sw_ver="3"
pl_h="No Data" name="Lounge" address="here" phone="555-5555"
country="US" city="LA"/>
<ns2:box id="7" mac="444444" d_a="2005" hw_ver="v1.0" sw_ver="3"
pl_h="No Data" name="MyHouse" address="there" phone="555-5556"
country="US" city="New York"/>
</ns2:boxes>

Is there some fundamental thing I am not getting? I cannot get
'tostrings' to work in ElementTree and I cannot figure the prolog out.

I posted a similar message back in January, but haven't had much luck.

PS
No I haven't been trying to do this since January, more important
things came up at work and I have just revived this. :)

Mar 28 '07 #1
8 2414
On Mar 28, 12:40 pm, "fscked" <fsckedag...@gmail.comwrote:
I am a beginning pythoner and I am having a terrible time trying to
figure out how to do something that (it would seeme to me) should be
fairly simple.

I have a CSV file of unknown encoding and I need to parse that file to
get the fields <--- DONE
I need to create an xml document that has the proper prolog and
namespace information in it. <--- NOT DONE
I need it to be encoded properly<--- Looks right in IE, not right in
any other app.

I should say that I have googled my butt off, tried ElementTree,
CSV2XML, and various other things and cannot get any of them to work.

A sample of the output I am looking for is as follows:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns2:boxes xmlns:ns2="Boxes">
<ns2:box id="9" mac="333333" d_a="2006" hw_ver="v1.1" sw_ver="3"
pl_h="No Data" name="Lounge" address="here" phone="555-5555"
country="US" city="LA"/>
<ns2:box id="7" mac="444444" d_a="2005" hw_ver="v1.0" sw_ver="3"
pl_h="No Data" name="MyHouse" address="there" phone="555-5556"
country="US" city="New York"/>
</ns2:boxes>

Is there some fundamental thing I am not getting? I cannot get
'tostrings' to work in ElementTree and I cannot figure the prolog out.

I posted a similar message back in January, but haven't had much luck.

PS
No I haven't been trying to do this since January, more important
things came up at work and I have just revived this. :)
I've never done this, but I found a recipe on the ActiveState website
that looks like it would be helpful:

http://aspn.activestate.com/ASPN/Coo.../Recipe/159100

I think you could modify it to make it work.

You could probably also use a combination of the csv module and the
pyxml module (links below).

http://pyxml.sourceforge.net/topics/
http://www.rexx.com/~dkuhlman/pyxmlfaq.html

I also found a Python XML book: http://www.oreilly.com/catalog/pytho...pter/ch01.html

I hope that helps. I've started my own adventure into XML with XRC and
wxPython.

Mike

Mar 28 '07 #2

ky******@gmail.com wrote:
<---SNIP--->
I've never done this, but I found a recipe on the ActiveState website
that looks like it would be helpful:

http://aspn.activestate.com/ASPN/Coo.../Recipe/159100

I tried looking at that but couldn't figure out how to get the
property file working.

I think you could modify it to make it work.

You could probably also use a combination of the csv module and the
pyxml module (links below).

http://pyxml.sourceforge.net/topics/
http://www.rexx.com/~dkuhlman/pyxmlfaq.html
These are a little too confusing for me. :)
I also found a Python XML book: http://www.oreilly.com/catalog/pytho...pter/ch01.html

I hope that helps. I've started my own adventure into XML with XRC and
wxPython.

Mike
Mar 28 '07 #3
En Wed, 28 Mar 2007 15:06:28 -0300, <ky******@gmail.comescribió:
You could probably also use a combination of the csv module and the
pyxml module (links below).

http://pyxml.sourceforge.net/topics/
Note the pyxml summary page on SourceForge: "PyXML is no longer
maintained."

--
Gabriel Genellina

Mar 28 '07 #4
fscked schrieb:
I am a beginning pythoner and I am having a terrible time trying to
figure out how to do something that (it would seeme to me) should be
fairly simple.
Show us code. As concise as possible. Then we might be able to help you.

Diez
Mar 28 '07 #5
Here is what I currently have. Still missing prolog information and
namespace info. Encoding is irritating me also. :)

import os,sys
import csv
from elementtree.ElementTree import Element, SubElement, ElementTree,
tostring

def indent(elem, level=0):
i = "\n" + level*" "
if len(elem):
if not elem.text or not elem.text.strip():
elem.text = i + " "
for elem in elem:
indent(elem, level+1)
if not elem.tail or not elem.tail.strip():
elem.tail = i
else:
if level and (not elem.tail or not elem.tail.strip()):
elem.tail = i

root = Element("boxes")
myfile = open('ClientsXMLUpdate.csv')
csvreader = csv.reader(myfile)

for row in csvreader:
mainbox = SubElement(root, "box")
r2 = csv.reader(myfile)
b = r2.next()
mainbox.attrib["city"] = b[10]
mainbox.attrib["country"] = b[9]
mainbox.attrib["phone"] = b[8]
mainbox.attrib["address"] = b[7]
mainbox.attrib["name"] = b[6]
mainbox.attrib["pl_heartbeat"] = b[5]
mainbox.attrib["sw_ver"] = b[4]
mainbox.attrib["hw_ver"] = b[3]
mainbox.attrib["date_activated"] = b[2]
mainbox.attrib["mac_address"] = b[1]
mainbox.attrib["boxid"] = b[0]

indent(root)
ElementTree(root).write('test.xml', "UTF-8")

Mar 29 '07 #6
Any ideas?

Apr 3 '07 #7
fscked schrieb:
I am a beginning pythoner and I am having a terrible time trying to
figure out how to do something that (it would seeme to me) should be
fairly simple.

I have a CSV file of unknown encoding and I need to parse that file to
get the fields <--- DONE
I need to create an xml document that has the proper prolog and
namespace information in it. <--- NOT DONE
I need it to be encoded properly<--- Looks right in IE, not right in
any other app.
UTF-8 encoding is the default. No need for a prologue here.

ET 1.3 will have an xml_declaration keyword argument for write() that will
allow you to write the declaration even if unnecessary. lxml already has it
now (and is ET compatible, so your code should just straight work).

http://codespeak.net/lxml

I should say that I have googled my butt off, tried ElementTree,
CSV2XML, and various other things and cannot get any of them to work.

A sample of the output I am looking for is as follows:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns2:boxes xmlns:ns2="Boxes">
<ns2:box id="9" mac="333333" d_a="2006" hw_ver="v1.1" sw_ver="3"
pl_h="No Data" name="Lounge" address="here" phone="555-5555"
country="US" city="LA"/>
<ns2:box id="7" mac="444444" d_a="2005" hw_ver="v1.0" sw_ver="3"
pl_h="No Data" name="MyHouse" address="there" phone="555-5556"
country="US" city="New York"/>
</ns2:boxes>
This should help you to get namespaces working:

http://effbot.org/zone/element.htm#xml-namespaces

Hope it helps,
Stefan
Apr 14 '07 #8
with lxml (although untested):

fscked wrote:
import os,sys
import csv
from lxml.etree import Element, SubElement, ElementTree, tostring
root = Element("{Boxes}boxes")
myfile = open('ClientsXMLUpdate.csv')
csvreader = csv.reader(myfile)

for row in csvreader:
mainbox = SubElement(root, "{Boxes}box")
r2 = csv.reader(myfile)
b = r2.next()
mainbox.put("city", b[10])
[...]

ElementTree(root).write('test.xml', "UTF-8", xml_declaration=True,
pretty_print=True)

Hope it helps,
Stefan
Apr 14 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Dominique | last post by:
I am trying to communicate to a prolog server from a java client, however even though the connection is successfully made every time I try to perform QueryExecute I get an error, either Socket...
0
by: Andrzej Walczak | last post by:
Dear all, I am looking for an interface to call SWI-Prolog from python which is my prototyping language. So far the best API looks to have pyprolog 0.2.0 from Nathan Denny. Code is a bit dated...
53
by: Andrew Poulos | last post by:
I've got some CSS that looks like this: body { margin: 0; font-family: Arial, Helvetica, sans-serif; font-size: 140.01%; color: #000000; } but IE won't apply the font size to text in table...
15
by: John Salerno | last post by:
Forgive my newbieness, but I don't quite understand why Unicode is still something that needs special treatment in Python (and perhaps elsewhere). I'm reading Dive Into Python right now, and it...
13
by: RK | last post by:
I apologize if this is a stupid question, I'm asking Python group for perspective on Ruby, but I don't see how the alternative of going to a ruby group for a perspective on Ruby is going to do me...
0
by: rodmc | last post by:
Can anyone recommend a library which will let me bridge between Python and Prolog? I have looked around but as yet have had no luck. Either the libraries are no longer being updated or the links do...
0
by: henk-jan ebbers | last post by:
Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.9 Precedence: list List-Id: General discussion...
7
by: Daniel | last post by:
what encoding does system.xml.xmldocument.save(string path) use to save the xml document if there is no <?xml... in the front of the xml document?
3
by: Eric_Dexter | last post by:
I am getting an error with pyswip on xp that says the .dll isn't installed as a shared library. Is there a manual way to install the .dll as such??? prolog seems to work fine it is just the...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.