Re: [Tutor] appending to a utf-16 encoded text file
Mark Tolonen wrote: "Tim Golden" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] Tim Brown wrote: Hi, I'm trying to create and append unicode strings to a utf-16 text file. The best I could come up with was to use codecs.open() with an encoding of 'utf-16' but when I do an append I get another UTF16 BOM put into the file which other programs do not expect to see :-( Is there some way to stop codecs from doing this or is there a better way to create and add data to a utf-16 text file? Well, there's nothing to stop you opening it "raw", as it were, and just appending unicode encoded as utf16. s = u"The cat sat on the mat" f = open ("utf16.txt", "wb") for word in s.split (): f.write (word.encode ("utf16") + " ") f.close () TJG Result: The@揾愀琀 sat@濾渀 the@淾愀琀 word.encode('utf16') adds a BOM every time, and the space wasn't encoded. utf-16-le and utf-16-be don't add the BOM. This works: import codecs s = u"The cat sat on the mat" f = codecs.open("utf16.txt","wb","utf-16-le") f.write(u'\ufeff') # if you want the BOM for word in s.split (): f.write (word + u' ') f.close() My apologies. I did run the code before posting, but I did no more than glance at the result in Notepad. Sorry. Should have used le/be as you've done. TJG ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] XML parsing with SAX was Re: (no subject)
"amit sethi" <[EMAIL PROTECTED]> wrote Please, always use a meaningful subject line when posting! and do not reply to an existing thread, changing the subject. These things mess up threaded reading tools such as gmane and newsreaders. This reduces your chances of a reply. Hi can any body give me an example as to how i can use Incremental Parser in xml.sax If you look in the XML Topic Guide section of the Python web site there is a full tutorial on using SAX. This is a good starting point: http://pyxml.sourceforge.net/topics/docs.html But frankly I'd strongly advise you to look at ElementTree if you are parsing XML. Its much easier to use. The only time I'd use sax nowadays is if I was porting code from some other sax based solution. http://effbot.org/zone/element-index.htm -- Alan Gauld Author of the Learn to Program web site http://www.freenetpages.co.uk/hp/alan.gauld ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] XML parsing with SAX
Sorry , I didn't follow the format of posting to the group. Well I am new to xml and python . What I basically want to do is to read elements from the xml which is by the way large ~2GB and read the attributes and make a decision as to whether i want to keep that element or not . I am told that xml.sax is good for reading a large xml file because it can read from a stream but what about writing the xml file back Thank you for your help. On Wed, Oct 22, 2008 at 1:49 PM, Alan Gauld <[EMAIL PROTECTED]>wrote: > "amit sethi" <[EMAIL PROTECTED]> wrote > > Please, always use a meaningful subject line when posting! > and do not reply to an existing thread, changing the subject. > These things mess up threaded reading tools such as gmane > and newsreaders. This reduces your chances of a reply. > > Hi can any body give me an example as to how i can use Incremental Parser >> in >> xml.sax >> > > If you look in the XML Topic Guide section of the Python web site there > is a full tutorial on using SAX. This is a good starting point: > > http://pyxml.sourceforge.net/topics/docs.html > > But frankly I'd strongly advise you to look at ElementTree if you are > parsing XML. Its much easier to use. The only time I'd use sax nowadays > is if I was porting code from some other sax based solution. > > http://effbot.org/zone/element-index.htm > > -- > Alan Gauld > Author of the Learn to Program web site > http://www.freenetpages.co.uk/hp/alan.gauld > > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > -- A-M-I-T S|S ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] XML parsing with SAX
amit sethi wrote: Sorry , I didn't follow the format of posting to the group. Well I am new to xml and python . What I basically want to do is to read elements from the xml which is by the way large ~2GB and read the attributes and make a decision as to whether i want to keep that element or not . I am told that xml.sax is good for reading a large xml file because it can read from a stream but what about writing the xml file back Thank you for your help. In general, we help with Python problems rather writing applications for free. You 'll either have to start paying somebody to do the work for you (in which case this is the wrong place to ask), or you'll have to make your hands dirty, and start reading/learning/coding/experimenting to solve your problem. When you get stuck, you can ask how to overcome that obstacle (usually by scaling the problem down to a small example), and we will try to find an answer. Sincerely, Albert ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] XML parsing with SAX
On Wed, Oct 22, 2008 at 9:07 AM, amit sethi <[EMAIL PROTECTED]> wrote: > Sorry , I didn't follow the format of posting to the group. Well I am new to > xml and python . What I basically want to do is to read elements from the > xml which is by the way large ~2GB and read the attributes and make a > decision as to whether i want to keep that element or not . I am told that > xml.sax is good for reading a large xml file because it can read from a > stream but what about writing the xml file back Thank you for your help. The iterparse() method of ElementTree might be useful, it allows you to prune the parsed tree as it is built: http://effbot.org/zone/element-iterparse.htm The resulting tree can be written back to a file using the write() method: http://effbot.org/zone/pythondoc-elementtree-ElementTree.htm#elementtree.ElementTree.ElementTree.write-method Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] threading not working how about fork?
Classification: UNCLASSIFIED Caveat (s): FOUO Thanks for the help and I am looking into the pyprocessing but threading is causing too many headaches (I may have to rewrite things). Lets say I have something as simple as below: def takeTime(a): print "Started %s" % a time.sleep(10) print "Ended %s" % a for each in [1,2,3,4,5]: pid = os.fork() takeTime(each) Each time the function is called it waits 10 seconds. And if I run it as above it does the first and waits 10 seconds then the second and waits ten seconds...etc. Wow could I get it to run through all 5 "forks" without waiting for the previous one to complete? That was all five would be done in about 10 seconds. This is simpler than my real problem but I can not even figure this one out. Thanks for the help. John Ertl Meteorologist FNMOC 7 Grace Hopper Ave. Monterey, CA 93943 (831) 656-5704 [EMAIL PROTECTED] Classification: UNCLASSIFIED Caveat (s): FOUO ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] threading not working how about fork?
On Wed, Oct 22, 2008 at 5:44 PM, Ertl, John C CIV 63134 <[EMAIL PROTECTED]> wrote: > Classification: UNCLASSIFIED > Caveat (s): FOUO ?? > Thanks for the help and I am looking into the pyprocessing but threading is > causing too many headaches (I may have to rewrite things). Lets say I have > something as simple as below: > > def takeTime(a): > print "Started %s" % a > time.sleep(10) > print "Ended %s" % a > > for each in [1,2,3,4,5]: >pid = os.fork() >takeTime(each) > > Each time the function is called it waits 10 seconds. And if I run it as > above it does the first and waits 10 seconds then the second and waits ten > seconds...etc. You are calling takeTime() from both the parent and child process, and you are spawning additional processes from the child process as well. Try if os.fork(): takeTime(each) break Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] decision structures
I hope we did not scare you away. Do you still want help? If you get a solution elsewhere would you share it with us so we can see and celebrate your progress? -- Bob Gailer Chapel Hill NC 919-636-4239 When we take the time to be aware of our feelings and needs we have more satisfying interatctions with others. Nonviolent Communication provides tools for this awareness. As a coach and trainer I can assist you in learning this process. What is YOUR biggest relationship challenge? ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor