Re: [Tutor] Regular Expression help
Thanks everyone for the replies all worked well, I adopted the string splitting approach in favour of the regex one as it seemed to miss less of the edge cases. I would like to thank everyone for their help once again -Original Message- From: Kent Johnson [mailto:[EMAIL PROTECTED] Sent: 27 June 2007 14:55 To: tutor@python.org; Gardner, Dean Subject: Re: [Tutor] Regular Expression help Gardner, Dean wrote: > Hi > > I have a text file that I would like to split up so that I can use it > in Excel to filter a certain field. However as it is a flat text file > I need to do some processing on it so that Excel can correctly import it. > > File Example: > tag descVR VM > (0012,0042) Clinical Trial Subject Reading ID LO 1 > (0012,0050) Clinical Trial Time Point ID LO 1 > (0012,0051) Clinical Trial Time Point Description ST 1 > (0012,0060) Clinical Trial Coordinating Center Name LO 1 > (0018,0010) Contrast/Bolus Agent LO 1 > (0018,0012) Contrast/Bolus Agent Sequence SQ 1 > (0018,0014) Contrast/Bolus Administration Route Sequence SQ 1 > (0018,0015) Body Part Examined CS 1 > > What I essentially want is to use python to process this file to give > me > > > (0012,0042); Clinical Trial Subject Reading ID; LO; 1 (0012,0050); > Clinical Trial Time Point ID; LO; 1 (0012,0051); Clinical Trial Time > Point Description; ST; 1 (0012,0060); Clinical Trial Coordinating > Center Name; LO; 1 (0018,0010); Contrast/Bolus Agent; LO; 1 > (0018,0012); Contrast/Bolus Agent Sequence; SQ ;1 (0018,0014); > Contrast/Bolus Administration Route Sequence; SQ; 1 (0018,0015); Body > Part Examined; CS; 1 > > so that I can import to excel using a delimiter. > > This file is extremely long and all I essentially want to do is to > break it into it 'fields' > > Now I suspect that regular expressions are the way to go but I have > only basic experience of using these and I have no idea what I should be doing. This seems to work: data = '''\ (0012,0042) Clinical Trial Subject Reading ID LO 1 (0012,0050) Clinical Trial Time Point ID LO 1 (0012,0051) Clinical Trial Time Point Description ST 1 (0012,0060) Clinical Trial Coordinating Center Name LO 1 (0018,0010) Contrast/Bolus Agent LO 1 (0018,0012) Contrast/Bolus Agent Sequence SQ 1 (0018,0014) Contrast/Bolus Administration Route Sequence SQ 1 (0018,0015) Body Part Examined CS 1'''.splitlines() import re fieldsRe = re.compile(r'^(\(\d+,\d+\)) (.*?) (\w+) (\d+)$') for line in data: match = fieldsRe.match(line) if match: print ';'.join(match.group(1, 2, 3, 4)) I don't think you want the space after the ; that you put in your example; Excel wants a single-character delimiter. Kent DISCLAIMER: Unless indicated otherwise, the information contained in this message is privileged and confidential, and is intended only for the use of the addressee(s) named above and others who have been specifically authorized to receive it. If you are not the intended recipient, you are hereby notified that any dissemination, distribution or copying of this message and/or attachments is strictly prohibited. The company accepts no liability for any damage caused by any virus transmitted by this email. Furthermore, the company does not warrant a proper and complete transmission of this information, nor does it accept liability for any delays. If you have received this message in error, please contact the sender and delete the message. Thank you. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] (no subject)
Please remove me from the mailing list. Thank you Jason P Bertrand JPB Enterprises www.businessloansandleasing.com (860) 982-5334 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] (no subject)
Jason Bertrand wrote: > Please remove me from the mailing list. You can do this yourself using the link at the bottom of each posting to the list. Kent > > Thank you > > Jason P Bertrand > JPB Enterprises > www.businessloansandleasing.com > (860) 982-5334 > > > > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] (no subject)
-BEGIN PGP SIGNED MESSAGE- Hash: SHA1 Please notice the link http://mail.python.org/mailman/listinfo/tutor in the footer of every mailing list mail. It's not really very nice to shoot into a big room with many persons in it "carry me out of the room". Most people prefer to walk out of the room by themselves. Andreas Jason Bertrand wrote: > Please remove me from the mailing list. > > Thank you > > Jason P Bertrand > JPB Enterprises > www.businessloansandleasing.com > (860) 982-5334 > > > > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor -BEGIN PGP SIGNATURE- Version: GnuPG v1.4.2 (GNU/Linux) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org iD8DBQFGhRaNHJdudm4KnO0RAvTiAJ9kLAhlYS/0L42UgDq/jL9PeZiC5ACfbOkk BGyRc1uWvr+DF0twaxSNj4M= =irHh -END PGP SIGNATURE- ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] (no subject)
On Fri, 2007-06-29 at 09:54 -0400, Jason Bertrand wrote: > Please remove me from the mailing list. > > Thank you > > Jason P Bertrand > JPB Enterprises > www.businessloansandleasing.com > (860) 982-5334 No one can do that except you. Look at the list headers to see how. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Fastest way to iterate through a file
"Robert Hicks" <[EMAIL PROTECTED]> wrote > This is the loop code: > > for line in f2: > for id in idList: > if id in line: > print "%s: %s" % (id, f2.next()) > found = "%s: %s" % (id, f2.next()) > f3.write(found) > While I note that you got a solution using regex one general point worth noting is that you should, in nested loop cases like this, at least use break in the inner loop. Here you search the line for each id - 129 searches, even if you find it first time. Thus adding a break inside the if block should save, on average 60 if tests. And given each test involves iterating over the line thats significant! Alan G. (Whos been away for a week) ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] database question
I've created a database as a shelve, where each value in the shelve file is a record class instance that has attributes representing fields. Let's say I enter 300 records in the shelve, then I decide to add a field to future records, or remove a field from future records. How can I update the existing records to include the new fields? Thanks, Luke -- "If you think that was good, wait 'til you taste the antidote!" ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Reading image dimensions before it is loaded from a web form using python.
Hello, I am writing an application that allows the user to upload an image to their folder. Is there a way to get the size of the file before it has been uploaded onto the server and give an error if the size does not comply to the maximum size. So far, my apps loads the image and checks it and then returns the error. Which is a bit pointless in that I should be able to get the file info before it is loaded, but I am not sure where to go. PIL reads the image size before it is loaded, but this is when I run this on my local machine. There is also a post http://mail.python.org/pipermail/python-list/2005-May/323018.html by Will McGugan where he loads 1px and rejects it if its too big. But his apps is an image scraper, so I suppose it fits his use case where he checks the size and then pulls the image if it fits. Anyone with ideas on how to deal with this. Thanks Norman -- Norman Khine ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Reading image dimensions before it is loaded from a web form using python.
Norman Khine wrote: > Hello, > I am writing an application that allows the user to upload an image to > their folder. > Is there a way to get the size of the file before it has been uploaded > onto the server and give an error if the size does not comply to the > maximum size. > [snip] > Anyone with ideas on how to deal with this. > You'll have to use javascript. Unless you want to tell the people "download and run x.py on your image to determine if it will be rejected!" if you want an auto check before they upload, you'll have to do client-side scripting. Python's not available for this. It only does server-side. This is fine, but if you're processing the image dimensions server-side, that means they already sent it to you. So look for a javascript image processing library. Alternatively, if by 'size' you meant 'filesize' you could do this much more simply (using Javascript) than having to process the actual image data to find the resolution. HTH, -Luke ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] database question
"Luke Jordan" <[EMAIL PROTECTED]> wrote > I've created a database as a shelve, where each value in the shelve > file is > a record class instance that has attributes representing fields. > Let's say I > enter 300 records in the shelve, then I decide to add a field to > future > records, or remove a field from future records. How can I update the > existing records to include the new fields? You will need to read in each record, update it and write it back again. A shelve is not a relational database so you cannot do mass updates in situ Alan G ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] DB switch
We are converting a database from Oracle to SQL 2005. We have a Python script that currently uses Digital Creation's 'DCOracle' python module. Any guidance on how to convert this for use with SQL 2005 is appreciated. Jeff ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Reading image dimensions before it is loaded from a web form using python.
Norman Khine wrote: > Hello, > I am writing an application that allows the user to upload an image to > their folder. > Is there a way to get the size of the file before it has been uploaded > onto the server and give an error if the size does not comply to the > maximum size. I'm not at all clear what your environment is. Is you application running on the computer where the image originates, or on the server? If your app is running on the source computer then you can just check the file size. If you have a web app and you are uploading from the browser then I don't know how to check. The trick in the email you reference might work. Kent > > So far, my apps loads the image and checks it and then returns the > error. Which is a bit pointless in that I should be able to get the file > info before it is loaded, but I am not sure where to go. > > PIL reads the image size before it is loaded, but this is when I run > this on my local machine. There is also a post > http://mail.python.org/pipermail/python-list/2005-May/323018.html by > Will McGugan where he loads 1px and rejects it if its too big. But his > apps is an image scraper, so I suppose it fits his use case where he > checks the size and then pulls the image if it fits. > > Anyone with ideas on how to deal with this. > > Thanks > > Norman > > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] DB switch
Smith, Jeff wrote: > We are converting a database from Oracle to SQL 2005. We have a Python > script that currently uses Digital Creation's 'DCOracle' python module. > Any guidance on how to convert this for use with SQL 2005 is appreciated. Presuming you mean MS-SQL Server, there are quite a few choices. See http://mail.python.org/pipermail/tutor/2006-September/049323.html http://mail.python.org/pipermail/tutor/2006-September/049326.html http://mail.python.org/pipermail/python-win32/2007-April/005727.html Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] 200 dollar questions!
I am a 4 day python newbie, and already I am writing practical codes. I want to learn as fast as possible so I am willing to pay for some tutorials. Please reply if you are interested, although 200 is not a lot of money. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor