Re: [Tutor] Replying
On 28/03/2011 23:17, Steven D'Aprano wrote: Corey Richardson wrote: Thunderbird has a reply list button that I use. It does? What version are you using? Also, if you're a keyboard person, Ctrl-Shift-L (Win7, TB 3.1.9) TJG ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] how to join two text files ?
Well...it looks like I do not know how to use it. Could You help me Example file_1: 20 53 2.66196 21 53 2.67512 20 52 2.63444 21 52 2.94148 Example file_2: 20 53 1.75904 21 53 2.92742 20 52 2.79653 21 52 2.12499 and so on my script: import glob {some code here} here is loop which change folder_daty, zmienna and folder_serw:## plik2=folder_daty+zmienna+XYZ.txt zbiorcze = folder_serw + zmienna + .txt if not os.path.isfile(zbiorcze): shutil.copy(plik2, zbiorcze) else: with open(zbiorcze, wb) as w: writer = csv.writer(w) for f in glob.glob(plik2): rows = open(f, rb).readlines() writer.writerows(rows) and result looks like: 2,0, ,5,3, ,2,.,4,4,8,2, 2,1, ,5,3, ,3,.,0,4,9,9,6, 2,0, ,5,2, ,3,.,1,8,4,9,5, 2,1, ,5,2, ,3,.,3,2,2,6,5, Hello, If the files are not too big, you could do something like: with open(/home/me/Desktop/test.csv, wb) as w: writer = csv.writer(w) for f in glob.glob(/home/me/Desktop/files/*.txt): rows = open(f, rb).readlines() writer.writerows(rows) Cheers!! Albert-Jan ~~ All right, but apart from the sanitation, the medicine, education, wine, public order, irrigation, roads, a fresh water system, and public health, what have the Romans ever done for us? ~~ *From:* Mateusz K km_w...@vp.pl *To:* tutor@python.org *Sent:* Sat, March 26, 2011 10:08:43 PM *Subject:* [Tutor] how to join two text files ? Hello, I have many text files, where data is delimited by space. Each file contain three colums: coordinate x, coordinate y and value for this location. I would like to join this data to get one big file which will contain columns: coordinate x, coordinate y, value1,value2,..., value_n and save it to another text file. I wrote script, but there's some stupid error (like \n character although I have added .rstrip() command). Coul You tell me what is most convenient way to join this data (maybe by using csv module?)? = Best regards, Mateusz ___ Tutor maillist - Tutor@python.org mailto:Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor -- *Pozdrawiam / Best regards, * Mateusz Ke;dzior | environmental engineer Uz.ywam otwartych http://pl.wikipedia.org/wiki/OpenDocument formatów plików biurowych environmental protection: please consider the environment before printing my email ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] finding directory of self
Rance Hall ran...@gmail.com wrote osname = os.name pathtocfg = os.path.dirname(sys.argv[0]) configfileloc = os.path.abspath(pathtocfg) os.chdir(configfileloc) to set the directory of all subsequent file lookups in a script. This is not the most user friendly thing to do. Some sys admins require config files (and indeed all data) to be located separately from programs. It would be more friendly to provide the option of defining the config file location, perhaps via an environment variable. Of course if the env variable is not defined you probably still need this code for the default location, in which case Wayne's suggestion should work. (Alternatively throw an error insisting the EV be set up) And if you are not deploying this to more than one operating envioronment where you know the policy then its a moot point anyway. Alan G. Back from his vacation :-) ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] server used in python
ema francis ema...@gmail.com wrote I am learnning python for 3 months from now. I wanted to know how and what *server* is used in python web development?Looking for your help Python is blessed with very many web development frameworks. For basic CGI programming you can use the basic weeb server that comes with Python. For deployment Apache or any other deployment scale server will do. If you are using the more complex frameworks they will have their own development/deployment recomendations, so it all depends on what framework you want to use. Everything from CherryPy to Zope or Plone... Your choice really. There is a good set of documents in the Python webn site that discuss the various web frameworks and options. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to read remote text file?
Ratna Banjara mast.ra...@gmail.com wrote I need to read text file from remote server and generate excel file from local computer using python. Is it possible? If so how? Which bit is hard? The reading the file remotely? Or the generating Excel? If the file system is mounted on your local computer you can read it like any other file via the mount. If not you will need to fetch a copy for which the server will need to provide some form of access mechanism - ftp, http, ssh or whatever. To convert the file to Excel will depend on what the original format is and how complex the output is. There are various support modules to help starting with the CSV module and ranging up to more Excel specific variants. Finally you can create/edit Excel documents directly via COM if you are on a Windows box with Excel installed... But that is not for the faint hearted! HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Problem recognizing '{' character?
Ben Hunter wrote: Hi, I'm completing the Python lessons on YouTube that Google posted. At the end of section 2 of day 2, there is a task to identify files then put them in a zip file in any directory. The code is from the 'solution' folder, so it's not something I wrote. I suspect I have a problem with PATHS or environment variables. I'm new to programming in something as advanced as Python, but I do okay with VBA - so I just feel like there's a setting up issue somewhere. I'm on Windows 7, tried running this in Idle and from the command line. The commands module used by zip_to() is an unfortunate choice for a tutorial because it is supposed to work with unix shells only. def zip_to(paths, zipfile): Zip up all of the given files into a new zip file with the given name. cmd = 'zip -j ' + zipfile + ' ' + ' '.join(paths) print Command I'm going to do: + cmd (status, output) = commands.getstatusoutput(cmd) # If command had a problem (status is non-zero), # print its output to stderr and exit. if status: sys.stderr.write(output) sys.exit(1) You can either try to install Cygwin to run your script unchanged or rewrite the above function to work with subprocess import sys from subprocess import Popen, PIPE def zip_to(paths, zipfile): command = [zip, -j, zipfile] command.extend(paths) process = Popen(command, stdout=PIPE, stderr=PIPE) stdoutdata, stderrdata = process.communicate() if process.returncode: sys.stdout.write(stdoutdata) sys.stderr.write(stderrdata) sys.exit(1) You'll still need a zip.exe on your system. If you're ambitious, have a look at http://docs.python.org/library/zipfile.html a library that allows you to create zipfiles in python without the help of an external program. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] how to join two text files ?
Mateusz K wrote: I have many text files, where data is delimited by space. Each file contain three colums: coordinate x, coordinate y and value for this location. I would like to join this data to get one big file which will contain columns: coordinate x, coordinate y, value1,value2,..., value_n and save it to another text file. I wrote script, but there's some stupid error (like \n character although I have added .rstrip() command). Coul You tell me what is most convenient way to join this data (maybe by using csv module?)? Please show us the code that you have. This proves that you've put some effort into the matter and allows us to focus on the missing parts. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Problem recognizing '{' character?
On 29/03/2011 09:41, Peter Otten wrote: Ben Hunter wrote: Hi, I'm completing the Python lessons on YouTube that Google posted. At the end of section 2 of day 2, there is a task to identify files then put them in a zip file in any directory. The code is from the 'solution' folder, so it's not something I wrote. I suspect I have a problem with PATHS or environment variables. I'm new to programming in something as advanced as Python, but I do okay with VBA - so I just feel like there's a setting up issue somewhere. I'm on Windows 7, tried running this in Idle and from the command line. The commands module used by zip_to() is an unfortunate choice for a tutorial because it is supposed to work with unix shells only. It's also unfortunate that an issue has been outstanding here for a while: http://bugs.python.org/issue10197 It's one of those which seems simple to fix but which spawns a thousand discussions... TJG ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] What's the logic behind parameters and arguments?
Dear list readers, the command find() takes two parameters, start and end, e.g.: find(substring[, start[, end]]). Here, a substring is located UP TO BUT NOT INCLUDING the optional parameter 'end'. Compare this to replace(). replace() comes with the count argument, e.g.: replace(old, new[, count]) But here the substring is replaced UP TO AND INCLUDING to the optional argument count. My question is how I am best to make sense of this discrepancy. Is there any logic behind this that might make my life easier once I become aware of it? I know of the indexing rules, but this here is obviously not the same. I am curious... Thanks, David ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] What's the logic behind parameters and arguments?
I don't see discrepancy, end and count are two arguments than mean very different things. End is the position where find ends, it could be included or excluded, in this case is excluded. Count is the maximun number of substrings you want to replace, it wouldn't make sense count=6 if you want to replace 5. 2011/3/29 David ld...@gmx.net Dear list readers, the command find() takes two parameters, start and end, e.g.: find(substring[, start[, end]]). Here, a substring is located UP TO BUT NOT INCLUDING the optional parameter 'end'. Compare this to replace(). replace() comes with the count argument, e.g.: replace(old, new[, count]) But here the substring is replaced UP TO AND INCLUDING to the optional argument count. My question is how I am best to make sense of this discrepancy. Is there any logic behind this that might make my life easier once I become aware of it? I know of the indexing rules, but this here is obviously not the same. I am curious... Thanks, David ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] What's the logic behind parameters and arguments?
Rafael Durán Castañeda rafadurancastan...@gmail.com wrote I don't see discrepancy, end and count are two arguments than mean very different things. End is the position where find ends, it could be included or excluded, in this case is excluded. Count is the maximun number of substrings you want to replace, it wouldn't make sense count=6 if you want to replace 5. And in general Python uses the convention for *positional* values that it goes up to but not including the last position. Compare slicing, range() etc. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] What's the logic behind parameters and arguments?
David wrote: Dear list readers, the command find() takes two parameters, start and end, e.g.: find(substring[, start[, end]]). Here, a substring is located UP TO BUT NOT INCLUDING the optional parameter 'end'. Compare this to replace(). replace() comes with the count argument, e.g.: replace(old, new[, count]) But here the substring is replaced UP TO AND INCLUDING to the optional argument count. My question is how I am best to make sense of this discrepancy. Is there any logic behind this that might make my life easier once I become aware of it? I know of the indexing rules, but this here is obviously not the same. I am curious... The two functions do different things, they work differently, they take different arguments. replace takes an inclusive `count` parameter because that's the most sensible and obvious way to implement a count parameter. I want to replace the first five words suggests a count parameter of 5, not 6. A count of 1 should replace 1 time, not 0 times. The start and end parameters of find work like slices, where the arguments act to slice *between* items: 0.1.2.3.4.5.6 |a|b|c|d|e|f| So, completely different, and there's no discrepancy. As for the question why replace doesn't take a start and end argument like find, *shrug* perhaps it should. But it already has three parameters, another two will start overloading it. -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Replying
Corey Richardson wrote: After inspecting the headers of emails from a few different lists, it appears: List-Id, Lust-Unsubscribe, List-Archive, List-Post, List-Help, Oh to be young again... I could have done with a Lust-Unsubscribe command quite a few times... -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Problem recognizing '{' character?
Thanks a ton. For the record I did read the 'command' module help page, but must have skipped over the 'Platforms: Unix' and 'Deprecated' parts. I successfully got it to work with the subprocess module, but I really appreciate you filling out the rest with the sys.stderr.write(stderrdata). I certainly would have stumbled over that. -BJH On Mon, Mar 28, 2011 at 8:12 PM, Ben Hunter bjameshun...@gmail.com wrote: Hi, I'm completing the Python lessons on YouTube that Google posted. At the end of section 2 of day 2, there is a task to identify files then put them in a zip file in any directory. The code is from the 'solution' folder, so it's not something I wrote. I suspect I have a problem with PATHS or environment variables. I'm new to programming in something as advanced as Python, but I do okay with VBA - so I just feel like there's a setting up issue somewhere. I'm on Windows 7, tried running this in Idle and from the command line. These two work perfectly. def get_special_paths(dirname): result = [] paths = os.listdir(dirname) # list of paths in that dir for fname in paths: match = re.search(r'__(\w+)__', fname) if match: result.append(os.path.abspath(os.path.join(dirname, fname))) return result def copy_to(paths, to_dir): if not os.path.exists(to_dir): os.mkdir(to_dir) for path in paths: fname = os.path.basename(path) shutil.copy(path, os.path.join(to_dir, fname)) This third one does not. def zip_to(paths, zipfile): Zip up all of the given files into a new zip file with the given name. cmd = 'zip -j ' + zipfile + ' ' + ' '.join(paths) print Command I'm going to do: + cmd (status, output) = commands.getstatusoutput(cmd) # If command had a problem (status is non-zero), # print its output to stderr and exit. if status: sys.stderr.write(output) sys.exit(1) My command is this: copyspecial.zip_to(paths, 'zippy') But something goes wrong and it spits this out: '{' is not recognized as an internal or external command, operable program or batch file. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Replying
Ok, thanks. I didn't think we should be replying to individuals unless on special case's. I also will have edit my header, but that's fine. -Original Message- From: Steven D'Aprano st...@pearwood.info Sent: Monday, March 28, 2011 6:14pm To: tutor@python.org Subject: Re: [Tutor] Replying markri...@gsoftcon.com wrote: When replying to the mailing list, does everyone just hit the reply button in your email program. Because that sends the email directly to your email. Also everyone is cc'ng the mailing list; is that the exceptable way to reply so everyone in the list gets the replies? Depends on the mail client I am using to reply. In mutt or kmail, I hit Reply to list, and the reply just goes to the list. In Thunderbird, I use Reply All, and edit the recipients by hand so that it just goes to the list, and curse the Thunderbird developers. You should not reply to the individual unless you have something private to tell them. Keep replies on the list, for the benefit of anyone else reading. Personally, I get annoyed when people CC me on replies that I'm also getting from the list, but I've long since stopped trying to hold the tide back :) Thank you for asking, and welcome! -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor Mark R Rivet, Genesis Software Consulting ASCT(Computer Technologies), BSIT/SE(Software Engineering) Electrical Engineering Technician Member IEEE, Computer Society Do or do not; there is no try. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] String formatting question.
Is there a difference (or preference) between using the following? %s %d % (var,num) VERSUS {0} {1}.format(var,num) Ramit Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology 712 Main Street | Houston, TX 77002 work phone: 713 - 216 - 5423 This communication is for informational purposes only. It is not intended as an offer or solicitation for the purchase or sale of any financial instrument or as an official confirmation of any transaction. All market prices, data and other information are not warranted as to completeness or accuracy and are subject to change without notice. Any comments or statements made herein do not necessarily reflect those of JPMorgan Chase Co., its subsidiaries and affiliates. This transmission may contain information that is privileged, confidential, legally privileged, and/or exempt from disclosure under applicable law. If you are not the intended recipient, you are hereby notified that any disclosure, copying, distribution, or use of the information contained herein (including any reliance thereon) is STRICTLY PROHIBITED. Although this transmission and any attachments are believed to be free of any virus or other defect that might affect any computer system into which it is received and opened, it is the responsibility of the recipient to ensure that it is virus free and no responsibility is accepted by JPMorgan Chase Co., its subsidiaries and affiliates, as applicable, for any loss or damage arising in any way from its use. If you received this transmission in error, please immediately contact the sender and destroy the material in its entirety, whether in electronic or hard copy format. Thank you. Please refer to http://www.jpmorgan.com/pages/disclosures for disclosures relating to European legal entities. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Grouping based on attributes of elements in a List
I have the following list List=[( 'G1', 'CFS', 'FCL', 'R1' ),('G3', 'LOOSEFREIGHT', 'MIXEDLCL', 'R9'), ('G4', 'CFS', 'FCL', 'R10' ), ('G2', 'LOOSEFREIGHT', 'LCL', 'R4' ), ('G1', 'CFS', 'FCL', 'R2' ), ('G2', 'LOOSEFREIGHT', 'LCL', 'R5') ] now I want to group this elements of List first by index [1] that is (CFS and LOOSEFREIGHT ) together and for those elements which are grouped together for LOOSEFREIGHT, i want to further divide them into different groups based on index[2] that is (LCL or MIXEDLCL) So essentially i want them grouped into different lists and my solution should be of the form New_List=[ [ ( 'G1', 'CFS', 'FCL', 'R1' ), ('G1', 'CFS', 'FCL', 'R2' ), ('G4', 'CFS', 'FCL', 'R10' ) ], [ ('G2', 'LOOSEFREIGHT', 'LCL', 'R4' ), ('G2', 'LOOSEFREIGHT', 'LCL', 'R5' )], [ ('G3', 'LOOSEFREIGHT', 'MIXEDLCL', 'R9')] ] How do I do it? I managed to do divide them into different lists based on index [1] however I was not able to further divide them based on index [2] Any help is appreciated ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] String formatting question.
On 03/29/2011 03:41 PM, Prasad, Ramit wrote: Is there a difference (or preference) between using the following? %s %d % (var,num) VERSUS {0} {1}.format(var,num) Ramit If you're using Python 3, use the second one. If you're using Python 2, you have no option but to use the first, as far as I know. Maybe Python 2.7 has that formatting, I'm not sure. -- Corey Richardson ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Grouping based on attributes of elements in a List
On 3/29/2011 4:03 PM, ranjan das wrote: I have the following list List=[( 'G1', 'CFS', 'FCL', 'R1' ),('G3', 'LOOSEFREIGHT', 'MIXEDLCL', 'R9'), ('G4', 'CFS', 'FCL', 'R10' ), ('G2', 'LOOSEFREIGHT', 'LCL', 'R4' ), ('G1', 'CFS', 'FCL', 'R2' ), ('G2', 'LOOSEFREIGHT', 'LCL', 'R5') ] now I want to group this elements of List first by index [1] that is (CFS and LOOSEFREIGHT ) together and for those elements which are grouped together for LOOSEFREIGHT, i want to further divide them into different groups based on index[2] that is (LCL or MIXEDLCL) So essentially i want them grouped into different lists and my solution should be of the form New_List=[ [ ( 'G1', 'CFS', 'FCL', 'R1' ), ('G1', 'CFS', 'FCL', 'R2' ), ('G4', 'CFS', 'FCL', 'R10' ) ], [ ('G2', 'LOOSEFREIGHT', 'LCL', 'R4' ), ('G2', 'LOOSEFREIGHT', 'LCL', 'R5' )], [ ('G3', 'LOOSEFREIGHT', 'MIXEDLCL', 'R9')] ] How do I do it? I managed to do divide them into different lists based on index [1] however I was not able to further divide them based on index [2] Any help is appreciated ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor You can you list comprehension (three times) checking for membership of the relevant items, or you can use for loop to go over all available tuples and sort them into different lists using if. In any case, after that you create a list of the three required lists. Eli ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] String formatting question.
On Tue, Mar 29, 2011 at 4:21 PM, Corey Richardson kb1...@aim.com wrote: On 03/29/2011 03:41 PM, Prasad, Ramit wrote: Is there a difference (or preference) between using the following? %s %d % (var,num) VERSUS {0} {1}.format(var,num) Ramit If you're using Python 3, use the second one. If you're using Python 2, you have no option but to use the first, as far as I know. Maybe Python 2.7 has that formatting, I'm not sure. -- Corey Richardson ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor you can use string{0}.format(var) in python 2.6. I use it all the time. I never use the other % method. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Run application from MS-DOS with sys.argv
Hello List: I developed a script to walk through a specific directory in my PC and look for files with the same extension (.shp). I want the user to enter from MS-DOS and write the the directory, file extension and csv filename. My script reads the arguments from Windows console, but when I opened the txt file and csv file I noticed that isn't walking through all the root I wrote in MS-DOS. This is my module: import os, csv, time, socket, sys from osgeo import ogr,gdal,osr #This should be the order for the arguments('csv_args.py [root] [file_extension] [csv filename]') #The user is typing python csv_args.py C:\ .shp csv.csv directorio = sys.argv[1] extension = sys.argv[2] csv_salida = sys.argv[3] if len(sys.argv) == 4: print 'Iniciando...' gdal.AllRegister() file_list = [] folders = None for root, folders, files in os.walk(directorio): file_list.extend(os.path.join(root,fi) for fi in files if fi.endswith(extension)) f = open(csv_salida, 'wb') log = open ('errores.txt','w') writer = csv.writer(f) ruta = 'Ruta' archivo = 'archivo' x_min = 'x_min' x_max = 'x_max' y_min = 'y_min' y_max = 'y_max' geometria = 'geometria' num_elem = 'num_elem' prj = '.prj' proyeccion = 'proyeccion' fecha = 'fecha_modificacion' maq = 'maquina_host' usu = 'usuario' campos = [ruta,archivo,x_min,x_max,y_min,y_max,geometria,num_elem,prj,proyeccion,fecha,maq,usu] writer.writerow(campos) for row, filepath in enumerate(file_list, start=1): (ruta, filename) = os.path.split(filepath) shapeData = ogr.Open(filepath) shp = 'Error al abrir el archivo' +filepath if shapeData is None: print shp log.write(shp+\n) else: layer = shapeData.GetLayer() feature = layer.GetNextFeature() x_y = layer.GetExtent() x1 = x_y[0] x2 = x_y[1] y1 = x_y[2] y2 = x_y[3] defn = layer.GetLayerDefn() geo = defn.GetGeomType() cuenta = layer.GetFeatureCount() proy = layer.GetSpatialRef() prjtext = ''+str(proy)+'' n = os.path.splitext(filepath) p = n[0]+'.prj' shx = n[0]+'.shx' dbf = n[0]+'.dbf' filepath = ''+filepath+'' filename = ''+filename+'' t = time.strftime(%m/%d/%Y %I:%M:%S %p,time.localtime(os.path.getmtime(filepath))) modificacion = ''+t+'' usuario = os.environ.get(USERNAME) user = ''+usuario+'' host = socket.gethostname() maquina = ''+host+'' if os.path.exists(shx): print 'El archivo ' +shx +' existe' else: og.write('No existe el archivo ' +shx+\n) if os.path.exists(dbf): print 'El archivo ' +dbf +' existe' else: log.write('No existe el archivo ' +dbf+\n) if os.path.exists(p): aRow= [ filepath, filename, x1, x2, y1, y2, geo, cuenta, 1, prjtext, modificacion, maquina, user] writer.writerow(aRow) else: aRow1= [ filepath, filename, x1, x2, y1, y2, geo, cuenta, 0, prjtext, modificacion, maquina, user] writer.writerow(aRow1) log.close() f.close() print El archivo esta listo else: print Tus argumentos no son correctos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Grouping based on attributes of elements in a List
On 29 March 2011 22:03, ranjan das ranjand2...@gmail.com wrote: List=[( 'G1', 'CFS', 'FCL', 'R1' ),('G3', 'LOOSEFREIGHT', 'MIXEDLCL', 'R9'), ('G4', 'CFS', 'FCL', 'R10' ), ('G2', 'LOOSEFREIGHT', 'LCL', 'R4' ), ('G1', 'CFS', 'FCL', 'R2' ), ('G2', 'LOOSEFREIGHT', 'LCL', 'R5') ] now I want to group this elements of List first by index [1] that is (CFS and LOOSEFREIGHT ) together and for those elements which are grouped together for LOOSEFREIGHT, i want to further divide them into different groups based on index[2] that is (LCL or MIXEDLCL) So essentially i want them grouped into different lists and my solution should be of the form New_List=[ [ ( 'G1', 'CFS', 'FCL', 'R1' ), ('G1', 'CFS', 'FCL', 'R2' ), ('G4', 'CFS', 'FCL', 'R10' ) ], [ ('G2', 'LOOSEFREIGHT', 'LCL', 'R4' ), ('G2', 'LOOSEFREIGHT', 'LCL', 'R5' )], [ ('G3', 'LOOSEFREIGHT', 'MIXEDLCL', 'R9')] ] How do I do it? You can use itemgetter from the operator module. The below should do what you want. I am using sorted to return a new list but you can also sort the list in place with list.sort(). import operator l =[( 'G1', 'CFS', 'FCL', 'R1' ),('G3', 'LOOSEFREIGHT', 'MIXEDLCL', 'R9'), ('G4', 'CFS', 'FCL', 'R10' ), ('G2', 'LOOSEFREIGHT', 'LCL', 'R4' ), ('G1', 'CFS', 'FCL', 'R2' ), ('G2', 'LOOSEFREIGHT', 'LCL', 'R5') ] sorted(l, key=operator.itemgetter(1,2)) [('G1', 'CFS', 'FCL', 'R1'), ('G4', 'CFS', 'FCL', 'R10'), ('G1', 'CFS', 'FCL', 'R2'), ('G2', 'LOOSEFREIGHT', 'LCL', 'R4'), ('G2', 'LOOSEFREIGHT', 'LCL', 'R5'), ('G3', 'LOOSEFREIGHT', 'MIXEDLCL', 'R9')] Greets Sander ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Grouping based on attributes of elements in a List
This would work nice too: list_=[( 'G1', 'CFS', 'FCL', 'R1' ),('G3', 'LOOSEFREIGHT', 'MIXEDLCL', 'R9'), ('G4', 'CFS', 'FCL', 'R10' ), ('G2', 'LOOSEFREIGHT', 'LCL', 'R4' ), ('G1', 'CFS', 'FCL', 'R2' ), ('G2', 'LOOSEFREIGHT', 'LCL', 'R5') ] sorted(list_,key=lambda l: l[1:3]) [('G1', 'CFS', 'FCL', 'R1'), ('G4', 'CFS', 'FCL', 'R10'), ('G1', 'CFS', 'FCL', 'R2'), ('G2', 'LOOSEFREIGHT', 'LCL', 'R4'), ('G2', 'LOOSEFREIGHT', 'LCL', 'R5'), ('G3', 'LOOSEFREIGHT', 'MIXEDLCL', 'R9')] And more pythonic, I think 2011/3/29 Sander Sweers sander.swe...@gmail.com On 29 March 2011 22:03, ranjan das ranjand2...@gmail.com wrote: List=[( 'G1', 'CFS', 'FCL', 'R1' ),('G3', 'LOOSEFREIGHT', 'MIXEDLCL', 'R9'), ('G4', 'CFS', 'FCL', 'R10' ), ('G2', 'LOOSEFREIGHT', 'LCL', 'R4' ), ('G1', 'CFS', 'FCL', 'R2' ), ('G2', 'LOOSEFREIGHT', 'LCL', 'R5') ] now I want to group this elements of List first by index [1] that is (CFS and LOOSEFREIGHT ) together and for those elements which are grouped together for LOOSEFREIGHT, i want to further divide them into different groups based on index[2] that is (LCL or MIXEDLCL) So essentially i want them grouped into different lists and my solution should be of the form New_List=[ [ ( 'G1', 'CFS', 'FCL', 'R1' ), ('G1', 'CFS', 'FCL', 'R2' ), ('G4', 'CFS', 'FCL', 'R10' ) ], [ ('G2', 'LOOSEFREIGHT', 'LCL', 'R4' ), ('G2', 'LOOSEFREIGHT', 'LCL', 'R5' )], [ ('G3', 'LOOSEFREIGHT', 'MIXEDLCL', 'R9')] ] How do I do it? You can use itemgetter from the operator module. The below should do what you want. I am using sorted to return a new list but you can also sort the list in place with list.sort(). import operator l =[( 'G1', 'CFS', 'FCL', 'R1' ),('G3', 'LOOSEFREIGHT', 'MIXEDLCL', 'R9'), ('G4', 'CFS', 'FCL', 'R10' ), ('G2', 'LOOSEFREIGHT', 'LCL', 'R4' ), ('G1', 'CFS', 'FCL', 'R2' ), ('G2', 'LOOSEFREIGHT', 'LCL', 'R5') ] sorted(l, key=operator.itemgetter(1,2)) [('G1', 'CFS', 'FCL', 'R1'), ('G4', 'CFS', 'FCL', 'R10'), ('G1', 'CFS', 'FCL', 'R2'), ('G2', 'LOOSEFREIGHT', 'LCL', 'R4'), ('G2', 'LOOSEFREIGHT', 'LCL', 'R5'), ('G3', 'LOOSEFREIGHT', 'MIXEDLCL', 'R9')] Greets Sander ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Grouping based on attributes of elements in a List
On 29 March 2011 23:52, Sander Sweers sander.swe...@gmail.com wrote: On 29 March 2011 22:03, ranjan das ranjand2...@gmail.com wrote: New_List=[ [ ( 'G1', 'CFS', 'FCL', 'R1' ), ('G1', 'CFS', 'FCL', 'R2' ), ('G4', 'CFS', 'FCL', 'R10' ) ], [ ('G2', 'LOOSEFREIGHT', 'LCL', 'R4' ), ('G2', 'LOOSEFREIGHT', 'LCL', 'R5' )], [ ('G3', 'LOOSEFREIGHT', 'MIXEDLCL', 'R9')] ] Hmm, looking at your New_list you actually want to sort on 3 indexes, on 1 then 0 then 2. So change the key= part from before to key=operator.itemgetter(1,0,2) and it will match your New_list perfectly. Greets Sander ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Grouping based on attributes of elements in a List
2011/3/29 Rafael Durán Castañeda rafadurancastan...@gmail.com: And more pythonic, I think I don't agree :-). I think itemgetter from the operator module is more flexible, readable and elegant than using a lamda. How would you sort on the first and last item with lambda? Greets Sander ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] server used in python
I think tornado (http://tornadoweb.org) is one of the easiest server / frameworks to learn and work with. On Tue, Mar 29, 2011 at 4:21 AM, Alan Gauld alan.ga...@btinternet.com wrote: ema francis ema...@gmail.com wrote I am learnning python for 3 months from now. I wanted to know how and what *server* is used in python web development?Looking for your help Python is blessed with very many web development frameworks. For basic CGI programming you can use the basic weeb server that comes with Python. For deployment Apache or any other deployment scale server will do. If you are using the more complex frameworks they will have their own development/deployment recomendations, so it all depends on what framework you want to use. Everything from CherryPy to Zope or Plone... Your choice really. There is a good set of documents in the Python webn site that discuss the various web frameworks and options. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Grouping based on attributes of elements in a List
From python docs: For non-negative indices, the length of a slice is the difference of the indices, if both are within bounds. For example, the length of word[1:3] is 2. Example: list_[1][1:3] ('LOOSEFREIGHT', 'MIXEDLCL') As I said i think my approach is more pythonic, but I'm not absolutely sure. With such approach I don't need importing, I use slicing and you are right, your approach is more flexible and would work on more cases, but in this particular case I still think unnecessary. Maybe someone else could tell us which is the best option. El 30 de marzo de 2011 00:14, Sander Sweers sander.swe...@gmail.comescribió: 2011/3/29 Rafael Durán Castañeda rafadurancastan...@gmail.com: And more pythonic, I think I don't agree :-). I think itemgetter from the operator module is more flexible, readable and elegant than using a lamda. How would you sort on the first and last item with lambda? Greets Sander ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Grouping based on attributes of elements in a List
Sander Sweers sander.swe...@gmail.com wrote flexible, readable and elegant than using a lamda. How would you sort on the first and last item with lambda? Wouldn't you just return a tuple of the two elements? Or am I missing something having jumped into the middle of the thread... Alan G. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] String formatting question.
On 29/03/2011 20:41, Prasad, Ramit wrote: Is there a difference (or preference) between using the following? %s %d % (var,num) VERSUS {0} {1}.format(var,num) Ramit ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor From Python 2.7.1 docs at http://docs.python.org/tutorial/inputoutput.html Since str.format() is quite new, a lot of Python code still uses the % operator. However, because this old style of formatting will eventually be removed from the language, str.format() should generally be used.. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Grouping based on attributes of elements in a List
On 30 March 2011 00:30, Alan Gauld alan.ga...@btinternet.com wrote: Wouldn't you just return a tuple of the two elements? Or am I missing something having jumped into the middle of the thread... Ah yes, you are correct. But imo reading key=lambda l: (l[1], l[0], l[2]) (which would be needed to sort how the OP wanted) hurts my eyes ;-). Anyway, both options will work fine for the OP. Greets Sander ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] String formatting question.
For simple strings I use the %s % foo version, for more complex stuff I use the .format() method. I find it easier to control spacing and alignments with the .format() method, but that's just me. -Modulok- On 3/29/11, Blockheads Oi Oi breamore...@yahoo.co.uk wrote: On 29/03/2011 20:41, Prasad, Ramit wrote: Is there a difference (or preference) between using the following? %s %d % (var,num) VERSUS {0} {1}.format(var,num) Ramit ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor From Python 2.7.1 docs at http://docs.python.org/tutorial/inputoutput.html Since str.format() is quite new, a lot of Python code still uses the % operator. However, because this old style of formatting will eventually be removed from the language, str.format() should generally be used.. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] (no subject)
I'm new to this list, so hello everybody!. The stuff: I'm working with regexps and this is my line: contents = re.sub(ul\/u, le ,contents) in perl there is a way to reference previous registers, i.e. $text =~ s/u(l|L|n|N)\/u/$1e/g; So I'm looking for the way to do it in python, obviously this does not works: contents = re.sub(u(l|L|n|N)\/u, $1e, contents) Thanks ___ andrés chandía P No imprima innecesariamente. ¡Cuide el medio ambiente! ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] String formatting question.
On Tue, Mar 29, 2011 at 2:41 PM, Prasad, Ramit ramit.pra...@jpmchase.comwrote: Is there a difference (or preference) between using the following? %s %d % (var,num) VERSUS {0} {1}.format(var,num) Practically there's no difference. In reality (and under the hood) there are more differences, some of which are subtle. For instance, in the first example, var = 3, num = 'hi' will error, while with .format, it won't. If you are writing code that should be backwards compatible, pre-2.6, then you should use the % formatting. My personal preference is to use .format() as it (usually) feels more elegant: ({0} *8+{1}).format(na, batman) vs: %s %s % (na * 8, batman) And named arguments: Name: {name}\nAddress: {address}.format(name=Bob, address=123 Castle Auuurrggh) vs Name: %(name)\nAddress: %(address) % {name: Bob, address, 123 Castle Auurgh) But when I'm dealing with floating point, especially if it's a simple output value, I will usually use % formatting: Money left: %8.2f % (money,) vs. Money Left: {0:8.2f).format(money) Of course, it's best to pick a style and stick to it - having something like this: print Name: %s % (name) print Address: {address}.format(address=street) is bad enough, but... print This is %s {0}.format(horrible) % (just) My recommendation would be to use what feels most natural to you. I think I read somewhere that % formatting is so ingrained that even though the .format() method is intended to replace it, it's probably going to stick around for a while. But if you want to be on the safe side, you can always just use .format() - it certainly won't hurt anything, and the fact that it says format is more explicit. If you didn't know Python, you would know that {0} {1} {2}.format(3,2,1) is doing some type of formatting, and since Explicit is better than implicit.*, that should be a good thing. HTH, Wayne * see: import this this.s.encode('rot13').split('\n')[3] ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor