minidom utf-8 encoding
Hi guys/gals. I am trying to write and xml file from data parsed from a csv. I can get everything to work except that I cannot get minidom to do --> ö which needless to say is driving me nuts. Any suggestions? What it ends up doing is just removing the character from the datastream. -- http://mail.python.org/mailman/listinfo/python-list
Re: minidom utf-8 encoding
Martin v. Löwis wrote:
> fscked schrieb:
> > Hi guys/gals.
> >
> > I am trying to write and xml file from data parsed from a csv.
> >
> > I can get everything to work except that I cannot get minidom to do -->
> > ö which needless to say is driving me nuts.
> >
> > Any suggestions?
>
> Works fine for me:
>
> py> d = minidom.Document()
> py> r = d.createElement("root")
> py> r.appendChild(d.createTextNode(u"\xf6"))
>
> py> d.appendChild(r)
>
> py> d.toxml()
> u'\n\xf6'
> py> print d.toxml()
>
> ö
>
> Regards,
> Martin
Well, let me clarify. If I just print it to the screen/console it works
fine, but when I do:
out.write( doc.toprettyxml())
it just removes the character that would be the "ö".
I can post the code if anyone wants to see it, but it is fairly
straightforward.
--
http://mail.python.org/mailman/listinfo/python-list
Re: minidom utf-8 encoding
Martin v. Löwis wrote:
<...snip...>
> I find that hard to believe. There is no code in Python that does
> removal of characters, and I can't see any other reason why it gets
> removed.
>
> OTOH, what I do get when writing to a file is a UnicodeError, when
> it tries to convert the Unicode string that toxml gives to a byte
> string.
>
> So I recommend you pass encoding="utf-8" to the toprettyxml invocation
> also.
>
> Regards,
> Martin
OK, now I am really confused. After trying all variations of opening
and writing and encoding and all the other voodoo I can find on the web
for hours, I decide to put the script back to how it was when it did
everything but remove the unicode characters.
And now it just works...
I hate it when that happens. In case you are wondering here is the code
that caused me all this (seemingly odd) pain:
import csv
import codecs
from xml.dom.minidom import Document
out = open("test.xml", "w")
# Create the minidom document
doc = Document()
# Create the base element
boxes = doc.createElement("boxes")
myfile = open('ClientsXMLUpdate.txt')
csvreader = csv.reader(myfile)
for row in csvreader:
mainbox = doc.createElement("box")
doc.appendChild(boxes)
r2 = csv.reader(myfile)
b = r2.next()
mainbox.setAttribute("city", b[10])
mainbox.setAttribute("country", b[9])
mainbox.setAttribute("phone", b[8])
mainbox.setAttribute("address", b[7])
mainbox.setAttribute("name", b[6])
mainbox.setAttribute("pl_heartbeat", b[5])
mainbox.setAttribute("sw_ver", b[4])
mainbox.setAttribute("hw_ver", b[3])
mainbox.setAttribute("date_activated", b[2])
mainbox.setAttribute("mac_address", b[1])
mainbox.setAttribute("boxid", b[0])
boxes.appendChild(mainbox)
# Print our newly created XML
out.write( doc.toprettyxml ())
And it just works...
--
http://mail.python.org/mailman/listinfo/python-list
set IP in linux with python
I have a text file with some network info like IP, mask, gateway, etc, that I want to set on a linux box. This script will run on the host that will be getting the new info. I can parse the data in the text file and get my data in a list, but I can't figure out how to put it in the right place of the config file. Any ideas? -- http://mail.python.org/mailman/listinfo/python-list
Re: set IP in linux with python
On Jun 12, 1:23 pm, fscked <[EMAIL PROTECTED]> wrote: > I have a text file with some network info like IP, mask, gateway, etc, > that I want to set on a linux box. This script will run on the host > that will be getting the new info. I can parse the data in the text > file and get my data in a list, but I can't figure out how to put it > in the right place of the config file. Any ideas? Nevermind. I ended up recreating thte files required rather than appending them. -- http://mail.python.org/mailman/listinfo/python-list
read list of dirnames and search for filenames
I cannot seem to get this to work. I am hyst trying to read in a list
of paths and see if the directory or any sub has a filename pattern.
Here is the code:
import os, sys
from path import path
myfile = open("boxids.txt", "r")
for line in myfile.readlines():
d = path(line)
for f in d.walkfiles('*Config*.xml'):
print f
And here is my error:
Traceback (most recent call last):
File "Untitled.py", line 21, in ?
for f in d.walkfiles('*Config*.xml'):
File "C:\Python24\Lib\site-packages\path.py", line 460, in walkfiles
childList = self.listdir()
File "C:\Python24\Lib\site-packages\path.py", line 328, in listdir
names = os.listdir(self)
WindowsError: [Errno 3] The system cannot find the path specified: u'X:
\\Instructions\\97544546294\n/*.*'
What I don't get is if I just print the path it prints correctly, but
it keeps adding double "\"s to it.
I tried changing the backslashies to forward slashies and I get :
WindowsError: [Errno 3] The system cannot find the path specified:
u'X:/Instructions/97544546294\n/*.*'
help?
--
http://mail.python.org/mailman/listinfo/python-list
Re: read list of dirnames and search for filenames
On May 1, 2:36 pm, Rob Wolfe <[EMAIL PROTECTED]> wrote:
> Rob Wolfe <[EMAIL PROTECTED]> writes:
> > fscked <[EMAIL PROTECTED]> writes:
>
> >> I cannot seem to get this to work. I am hyst trying to read in a list
> >> of paths and see if the directory or any sub has a filename pattern.
> >> Here is the code:
>
> >> import os, sys
> >> from path import path
>
> >> myfile = open("boxids.txt", "r")
> >> for line in myfile.readlines():
>
> And you don't need to use ``readlines`` at all.
> This is enough:
>
> for line in myfile:
>
> --
> HTH,
> Rob
Worked well, thanks!
--
http://mail.python.org/mailman/listinfo/python-list
_csv.Error: string with NUL bytes
Anyone have an idea of what I might do to fix this? I have googled adn can only find some random conversations about it that doesn't make sense to me. I am basically reading in a csv file to create an xml and get this error. I don't see any empty values in any fields or anything... -- http://mail.python.org/mailman/listinfo/python-list
Re: _csv.Error: string with NUL bytes
On May 3, 9:11 am, Larry Bates <[EMAIL PROTECTED]> wrote:
> fscked wrote:
> > Anyone have an idea of what I might do to fix this? I have googled adn
> > can only find some random conversations about it that doesn't make
> > sense to me.
>
> > I am basically reading in a csv file to create an xml and get this
> > error.
>
> > I don't see any empty values in any fields or anything...
>
> You really should post some code and the actual traceback error your
> get for us to help. I suspect that you have an ill-formed record in
> your CSV file. If you can't control that, you may have to write your
> own CSV dialect parser.
>
> -Larry
Certainly, here is the code:
import os,sys
import csv
from elementtree.ElementTree import Element, SubElement, ElementTree
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}boxes")
myfile = open('test.csv', 'rb')
csvreader = csv.reader(myfile)
for boxid, mac, activated, hw_ver, sw_ver, heartbeat, name, address,
phone, country, city, in csvreader:
mainbox = SubElement(root, "{Boxes}box")
mainbox.attrib["city"] = city
mainbox.attrib["country"] = country
mainbox.attrib["phone"] = phone
mainbox.attrib["address"] = address
mainbox.attrib["name"] = name
mainbox.attrib["pl_heartbeat"] = heartbeat
mainbox.attrib["sw_ver"] = sw_ver
mainbox.attrib["hw_ver"] = hw_ver
mainbox.attrib["date_activated"] = activated
mainbox.attrib["mac_address"] = mac
mainbox.attrib["boxid"] = boxid
indent(root)
ElementTree(root).write('test.xml', encoding='UTF-8')
The traceback is as follows:
Traceback (most recent call last):
File "createXMLPackage.py", line 35, in ?
for boxid, mac, activated, hw_ver, sw_ver, heartbeat, name,
address, phone, country, city, in csvreader:
_csv.Error: string with NUL bytes
Exit code: 1 , 0001h
--
http://mail.python.org/mailman/listinfo/python-list
Re: _csv.Error: string with NUL bytes
On May 3, 9:29 am, Marc 'BlackJack' Rintsch <[EMAIL PROTECTED]> wrote: > In <[EMAIL PROTECTED]>, fscked wrote: > > The traceback is as follows: > > > Traceback (most recent call last): > > File "createXMLPackage.py", line 35, in ? > > for boxid, mac, activated, hw_ver, sw_ver, heartbeat, name, > > address, phone, country, city, in csvreader: > > _csv.Error: string with NUL bytes > > Exit code: 1 , 0001h > > As Larry said, this most likely means there are null bytes in the CSV file. > > Ciao, > Marc 'BlackJack' Rintsch How would I go about identifying where it is? -- http://mail.python.org/mailman/listinfo/python-list
path stuff
I am walking some directories looking for a certain filename pattern. This part works fine, but what if I want to exclude results from a certain directory being printed? eg d:\dir\mydir1\filename.txt <--I want to see this one d:\dir\mydir2\archived\filename.txt <--I don't want to see anything in the "archived" directory d:\dir\mydir2\filename.txt <--Again, I do want to see this one I am having a bit of trouble figuring out how to use the path module to hack up the path to determine if I am in a subdir I care about. So either don show me the results from a certain directory or just plain skip a certain directory. -- http://mail.python.org/mailman/listinfo/python-list
Re: path stuff
On May 9, 7:02 pm, "Gabriel Genellina" <[EMAIL PROTECTED]> wrote:
> En Wed, 09 May 2007 15:11:06 -0300, fscked <[EMAIL PROTECTED]>
> escribió:
>
> > I am walking some directories looking for a certain filename pattern.
> > This part works fine, but what if I want to exclude results from a
> > certain directory being printed?
>
> Using os.walk you can skip undesired directories entirely:
>
> for dirpath, dirnames, filenames in os.walk(starting_dir):
> if "archived" in dirnames:
> dirnames.remove("archived")
> # process filenames, typically:
> for filename in filenames:
> fullfn = os.path.join(dirpath, filename)
> ...
>
> --
> Gabriel Genellina
OK, this is on Winbloze and it keeps giving me "The directory name is
invalid: u"blahblahblah" with double backslashies everywhere. I am
currently trying to figure out how to make those go away. I shall
check back in a bit.
thanks for all the help so far. :)
--
http://mail.python.org/mailman/listinfo/python-list
Re: path stuff
On May 10, 10:41 am, fscked <[EMAIL PROTECTED]> wrote:
> On May 9, 7:02 pm, "Gabriel Genellina" <[EMAIL PROTECTED]> wrote:
>
>
>
>
>
> > En Wed, 09 May 2007 15:11:06 -0300, fscked <[EMAIL PROTECTED]>
> > escribió:
>
> > > I am walking some directories looking for a certain filename pattern.
> > > This part works fine, but what if I want to exclude results from a
> > > certain directory being printed?
>
> > Using os.walk you can skip undesired directories entirely:
>
> > for dirpath, dirnames, filenames in os.walk(starting_dir):
> > if "archived" in dirnames:
> > dirnames.remove("archived")
> > # process filenames, typically:
> > for filename in filenames:
> > fullfn = os.path.join(dirpath, filename)
> > ...
>
> > --
> > Gabriel Genellina
>
> OK, this is on Winbloze and it keeps giving me "The directory name is
> invalid: u"blahblahblah" with double backslashies everywhere. I am
> currently trying to figure out how to make those go away. I shall
> check back in a bit.
>
> thanks for all the help so far. :)- Hide quoted text -
>
> - Show quoted text -
ok, got the backslashies fixed, not I want it to print just a single
line for each matching filename and dirpath, but it prints 3... hmm...
--
http://mail.python.org/mailman/listinfo/python-list
Re: path stuff
On May 10, 12:45 pm, fscked <[EMAIL PROTECTED]> wrote:
> On May 10, 10:41 am, fscked <[EMAIL PROTECTED]> wrote:
>
>
>
>
>
> > On May 9, 7:02 pm, "Gabriel Genellina" <[EMAIL PROTECTED]> wrote:
>
> > > En Wed, 09 May 2007 15:11:06 -0300, fscked <[EMAIL PROTECTED]>
> > > escribió:
>
> > > > I am walking some directories looking for a certain filename pattern.
> > > > This part works fine, but what if I want to exclude results from a
> > > > certain directory being printed?
>
> > > Using os.walk you can skip undesired directories entirely:
>
> > > for dirpath, dirnames, filenames in os.walk(starting_dir):
> > > if "archived" in dirnames:
> > > dirnames.remove("archived")
> > > # process filenames, typically:
> > > for filename in filenames:
> > > fullfn = os.path.join(dirpath, filename)
> > > ...
>
> > > --
> > > Gabriel Genellina
>
> > OK, this is on Winbloze and it keeps giving me "The directory name is
> > invalid: u"blahblahblah" with double backslashies everywhere. I am
> > currently trying to figure out how to make those go away. I shall
> > check back in a bit.
>
> > thanks for all the help so far. :)- Hide quoted text -
>
> > - Show quoted text -
>
> ok, got the backslashies fixed, not I want it to print just a single
> line for each matching filename and dirpath, but it prints 3... hmm...- Hide
> quoted text -
>
> - Show quoted text -
Nevermind, I am indentationally challenged. I was printing under the
for dirpath, dirname, filename part and had to unindent uno time.
It works as desired now, thanks!
--
http://mail.python.org/mailman/listinfo/python-list
Re: path stuff
On May 10, 1:43 pm, fscked <[EMAIL PROTECTED]> wrote:
> On May 10, 12:45 pm, fscked <[EMAIL PROTECTED]> wrote:
>
>
>
>
>
> > On May 10, 10:41 am, fscked <[EMAIL PROTECTED]> wrote:
>
> > > On May 9, 7:02 pm, "Gabriel Genellina" <[EMAIL PROTECTED]> wrote:
>
> > > > En Wed, 09 May 2007 15:11:06 -0300, fscked <[EMAIL PROTECTED]>
> > > > escribió:
>
> > > > > I am walking some directories looking for a certain filename pattern.
> > > > > This part works fine, but what if I want to exclude results from a
> > > > > certain directory being printed?
>
> > > > Using os.walk you can skip undesired directories entirely:
>
> > > > for dirpath, dirnames, filenames in os.walk(starting_dir):
> > > > if "archived" in dirnames:
> > > > dirnames.remove("archived")
> > > > # process filenames, typically:
> > > > for filename in filenames:
> > > > fullfn = os.path.join(dirpath, filename)
> > > > ...
>
> > > > --
> > > > Gabriel Genellina
>
> > > OK, this is on Winbloze and it keeps giving me "The directory name is
> > > invalid: u"blahblahblah" with double backslashies everywhere. I am
> > > currently trying to figure out how to make those go away. I shall
> > > check back in a bit.
>
> > > thanks for all the help so far. :)- Hide quoted text -
>
> > > - Show quoted text -
>
> > ok, got the backslashies fixed, not I want it to print just a single
> > line for each matching filename and dirpath, but it prints 3... hmm...-
> > Hide quoted text -
>
> > - Show quoted text -
>
> Nevermind, I am indentationally challenged. I was printing under the
> for dirpath, dirname, filename part and had to unindent uno time.
>
> It works as desired now, thanks!- Hide quoted text -
>
> - Show quoted text -
ok, I lied, it is still doing the archived folders. Here is the code:
import os, sys
from path import path
myfile = open("boxids.txt", "r", 0)
for line in myfile:
d = 'D:\\Dir\\' + path(line.strip())
for f in d.walkfiles('*Config*.xml'):
for dirpath, dirnames, filenames in os.walk(d):
if "Archived" in dirnames:
dirnames.remove("Archived") #skip this directory
print f
print 'Done'
when it does the print f it still shows me the dirs i don't want to
see.
any more ideas?
TIA
--
http://mail.python.org/mailman/listinfo/python-list
Re: path stuff
On May 10, 6:08 pm, "Gabriel Genellina" <[EMAIL PROTECTED]>
wrote:
> En Thu, 10 May 2007 19:04:30 -0300, fscked <[EMAIL PROTECTED]>
> escribió:
>
>
>
>
>
> > ok, I lied, it is still doing the archived folders. Here is the code:
>
> > import os, sys
> > from path import path
>
> > myfile = open("boxids.txt", "r", 0)
> > for line in myfile:
> > d = 'D:\\Dir\\' + path(line.strip())
> > for f in d.walkfiles('*Config*.xml'):
> > for dirpath, dirnames, filenames in os.walk(d):
> > if "Archived" in dirnames:
> > dirnames.remove("Archived") #skip this directory
> > print f
> > print 'Done'
>
> > when it does the print f it still shows me the dirs i don't want to
> > see.
>
> You are walking the directory structure *twice*, using two different
> methods at the same time. Also, there is no standard `path` module, and
> several implementations around, so it would be a good idea to tell us
> which one you use.
> If you want to omit a directory, and include just filenames matching a
> pattern:
>
> import os, sys, os.path, fnmatch
>
> def findinterestingfiles(root_dir):
>for dirpath, dirnames, filenames in os.walk(root_dir):
> if "Archived" in dirnames:
>dirnames.remove("Archived")
> for filename in fnmatch.filter(filenames, '*Config*.xml'):
>fullfn = os.path.join(dirpath, filename)
>print fullfn
>
> myfile = open("boxids.txt", "r")
> for line in myfile:
>dirname = os.path.join('D:\\Dir\\', line.strip())
>findinterestingfiles(dirname):
> myfile.close()
>
> --
> Gabriel Genellina- Hide quoted text -
>
> - Show quoted text -
Should this code work? I get a syntax error and cannot figure out why.
--
http://mail.python.org/mailman/listinfo/python-list
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: 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. :) -- http://mail.python.org/mailman/listinfo/python-list
Re: XML/encoding/prolog/python hell...
[EMAIL PROTECTED] 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/Cookbook/Python/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/pythonxml/chapter/ch01.html > > I hope that helps. I've started my own adventure into XML with XRC and > wxPython. > > Mike -- http://mail.python.org/mailman/listinfo/python-list
Re: XML/encoding/prolog/python hell...
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")
--
http://mail.python.org/mailman/listinfo/python-list
Re: XML/encoding/prolog/python hell...
Any ideas? -- http://mail.python.org/mailman/listinfo/python-list
ValueError: too many values to unpack
Trying to use CSV to read in a line with 11 fields and I keep getting this error. I have googled a bit and have been unable to figure it out. -- http://mail.python.org/mailman/listinfo/python-list
Re: ValueError: too many values to unpack
On Apr 11, 10:26 am, Laszlo Nagy <[EMAIL PROTECTED]> wrote:
> fscked írta:> Trying to use CSV to read in a line with 11 fields and I keep
> getting
> > this error. I have googled a bit and have been unable to figure it out.
>
> Probably you have more than 11 values in some (or all) of the rows in
> the CSV file. Try this code:
>
> L = (1,2,3,4,5)
> a1,a2,a3 = L
>
> If you are sure that you only need a certain number of values, "the
> first N columns":
>
> a1,a2,a3 = L[:3]
>
> Then you still can have a "not enough values to unpack" error, guess
> what that means. ;-)
>
>Laszlo
Hmm, well I have counted the fields in the CSV and verified there are
only 11. Here is the offending code:
myfile = open('ClientsXMLUpdate.csv')
csvreader = csv.reader(myfile)
for boxid, mac, activated, hw_ver, sw_ver, heartbeat, name, address,
phone, country, city in csvreader:
mainbox = SubElement(root, "{Boxes}box")
mainbox.attrib["city"] = city
mainbox.attrib["country"] = country
mainbox.attrib["phone"] = phone
mainbox.attrib["address"] = address
mainbox.attrib["name"] = name
mainbox.attrib["pl_heartbeat"] = heartbeat
mainbox.attrib["sw_ver"] = sw_ver
mainbox.attrib["hw_ver"] = hw_ver
mainbox.attrib["date_activated"] = activated
mainbox.attrib["mac_address"] = mac
mainbox.attrib["boxid"] = boxid
I just don't get it... :/
--
http://mail.python.org/mailman/listinfo/python-list
Re: ValueError: too many values to unpack
You guys have given me some great ideas, I am going to try them all out and let you guys know how it turns out. On a side note, thanks for nto bashing a noob like me who isn't the greatest pythonista around. I am trying to learn and you guys are how I learn my mistakes. Well you, and the fact the stuff occasionally doesn't work. :) Thanks again and I will report back in a couple hours (meetings). -- http://mail.python.org/mailman/listinfo/python-list
XLM prolgoue
How do I go about creating the XML prologue like I want it to be? Specifically, I am trying to add encoding and some namespace stuff. -- http://mail.python.org/mailman/listinfo/python-list
