Re: [Tutor] compare and arrange file
On Wed, Jul 13, 2011 at 11:02 AM, Peter Otten __pete...@web.de wrote: Edgar Almonte wrote: fist time i saw the statement with , is part of the module csv ? , that make a loop through the file ? is not the sortkey function waiting for a paramenter ( the row ) ? i don't see how is get pass , the key is a parameter of sorted function ? , reader is a function of csv module ? if with... is a loop that go through the file is the second with inside of that loop ? ( i dont see how the write of the output file catch the line readed with open(filename) as fileobj: do_something_with(fileobj) is a shortcut for fileobj = open(filename) do_something_with(fileobj) fileobj.close() But it is not only shorter; it also guarantees that the file will be closed even if an error occurs while it is being processed. In my code the file object is wrapped into a csv.reader. for row in csv.reader(fileobj, delimiter=|): # do something with row walks through the file one row at a time where the row is a list of the columns. To be able to sort these rows you have to read them all into memory. You typically do that with rows_iter = csv.reader(fileobj, delimiter=|) rows = list(rows_iter) and can then sort the rows with rows.sort() Because this two-step process is so common there is a builtin sorted() that converts an iterable (the rows here) into a sorted list. Now consider the following infile: $ cat infile.txt a | 0.00| 1.11| b | 0.00| 1.11| X | 0.00| 88115.39| X | 90453.29| 0.00| X | 0.00| 90443.29| c | 1.11| 0.00| X | 88115.39| 0.00| X | 0.00| 88335.39| X | 90453.29| 0.00| X | 88335.39| 0.00| X | 90443.29| 0.00| d | 1.11| 0.00| If we read it and sort it we get the following: import csv with open(infile.txt) as fileobj: ... rows = sorted(csv.reader(fileobj, delimiter=|)) ... from pprint import pprint pprint(rows) [['X ', ' 0.00', ' 88115.39', ''], ['X ', ' 0.00', ' 88335.39', ''], ['X ', ' 0.00', ' 90443.29', ''], ['X ', ' 88115.39', ' 0.00', ''], ['X ', ' 88335.39', ' 0.00', ''], ['X ', ' 90443.29', ' 0.00', ''], ['X ', ' 90453.29', ' 0.00', ''], ['X ', ' 90453.29', ' 0.00', ''], ['a ', ' 0.00', ' 1.11', ''], ['b ', ' 0.00', ' 1.11', ''], ['c ', ' 1.11', ' 0.00', ''], ['d ', ' 1.11', ' 0.00', '']] Can you infer the sort order of the list of lists above? The rows are sorted by the first item in the list, then rows whose first item compares equal are sorted by the second and so on. This sort order is not something built into the sort() method, but rather the objects that are compared. sort() uses the usual operators like and == internally. Now what would you do if you wanted to sort your data by the third column, say? Here the key parameter comes into play. You can use it to provide a function that takes an item in the list to be sorted and returns something that is used instead of the items to compare them to each other: def extract_third_column(row): ... return row[2] ... rows.sort(key=extract_third_column) pprint(rows) [['X ', ' 88115.39', ' 0.00', ''], ['X ', ' 88335.39', ' 0.00', ''], ['X ', ' 90443.29', ' 0.00', ''], ['X ', ' 90453.29', ' 0.00', ''], ['X ', ' 90453.29', ' 0.00', ''], ['c ', ' 1.11', ' 0.00', ''], ['d ', ' 1.11', ' 0.00', ''], ['a ', ' 0.00', ' 1.11', ''], ['b ', ' 0.00', ' 1.11', ''], ['X ', ' 0.00', ' 88115.39', ''], ['X ', ' 0.00', ' 88335.39', ''], ['X ', ' 0.00', ' 90443.29', '']] The key function you actually need is a bit more sophisticated. You want rows with equal nonzero values to end close together, no matter whether the interesting value is in the second or third column. Let's try: def extract_nonzero_column(row): ... if row[1] == ' 0.00': ... return
Re: [Tutor] compare and arrange file
On Tue, Jul 12, 2011 at 10:32 PM, Emile van Sebille em...@fenx.com wrote: On 7/12/2011 4:01 PM Edgar Almonte said... On Tue, Jul 12, 2011 at 5:44 AM, Peter Otten__pete...@web.de wrote: snip import csv imports the comma separated values (csv) file handler utilities module def sortkey(row): if float(row[1]): return row[1], True else: return row[2], False ... sortkey defines a function that accepts a cvs.reader data row, and returns either row[1] and True or row[2] and False based on which of row[1] and row[2] has a non-zero value with open(infile.txt, rb) as instream: rows = sorted(csv.reader(instream, delimiter=|), key=sortkey) rows becomes the sortkey sorted result of the lines of infile.txt with open(outfile.txt, wb) as outstream: csv.writer(outstream, delimiter=|).writerows(rows) ... and this writes those results to outfile.txt you might also try the shortened: from csv import reader,writer def sortkey(row): return max(row[1],row[2]),row[1]row[2] writer(open(outfile.txt, wb), delimiter=|).writerows( sorted(reader(open(infile.txt, rb), delimiter=|),key=sortkey)) Emile ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor fist time i saw the statement with , is part of the module csv ? , that make a loop through the file ? is not the sortkey function waiting for a paramenter ( the row ) ? i don't see how is get pass , the key is a parameter of sorted function ? , reader is a function of csv module ? if with... is a loop that go through the file is the second with inside of that loop ? ( i dont see how the write of the output file catch the line readed Thanks again for the helping of my understand ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] compare and arrange file
Edgar Almonte wrote: fist time i saw the statement with , is part of the module csv ? , that make a loop through the file ? is not the sortkey function waiting for a paramenter ( the row ) ? i don't see how is get pass , the key is a parameter of sorted function ? , reader is a function of csv module ? if with... is a loop that go through the file is the second with inside of that loop ? ( i dont see how the write of the output file catch the line readed with open(filename) as fileobj: do_something_with(fileobj) is a shortcut for fileobj = open(filename) do_something_with(fileobj) fileobj.close() But it is not only shorter; it also guarantees that the file will be closed even if an error occurs while it is being processed. In my code the file object is wrapped into a csv.reader. for row in csv.reader(fileobj, delimiter=|): # do something with row walks through the file one row at a time where the row is a list of the columns. To be able to sort these rows you have to read them all into memory. You typically do that with rows_iter = csv.reader(fileobj, delimiter=|) rows = list(rows_iter) and can then sort the rows with rows.sort() Because this two-step process is so common there is a builtin sorted() that converts an iterable (the rows here) into a sorted list. Now consider the following infile: $ cat infile.txt a | 0.00| 1.11| b | 0.00| 1.11| X | 0.00| 88115.39| X | 90453.29| 0.00| X | 0.00| 90443.29| c | 1.11| 0.00| X | 88115.39| 0.00| X | 0.00| 88335.39| X | 90453.29| 0.00| X | 88335.39| 0.00| X | 90443.29| 0.00| d | 1.11| 0.00| If we read it and sort it we get the following: import csv with open(infile.txt) as fileobj: ... rows = sorted(csv.reader(fileobj, delimiter=|)) ... from pprint import pprint pprint(rows) [['X ', ' 0.00', ' 88115.39', ''], ['X ', ' 0.00', ' 88335.39', ''], ['X ', ' 0.00', ' 90443.29', ''], ['X ', ' 88115.39', ' 0.00', ''], ['X ', ' 88335.39', ' 0.00', ''], ['X ', ' 90443.29', ' 0.00', ''], ['X ', ' 90453.29', ' 0.00', ''], ['X ', ' 90453.29', ' 0.00', ''], ['a ', ' 0.00', ' 1.11', ''], ['b ', ' 0.00', ' 1.11', ''], ['c ', ' 1.11', ' 0.00', ''], ['d ', ' 1.11', ' 0.00', '']] Can you infer the sort order of the list of lists above? The rows are sorted by the first item in the list, then rows whose first item compares equal are sorted by the second and so on. This sort order is not something built into the sort() method, but rather the objects that are compared. sort() uses the usual operators like and == internally. Now what would you do if you wanted to sort your data by the third column, say? Here the key parameter comes into play. You can use it to provide a function that takes an item in the list to be sorted and returns something that is used instead of the items to compare them to each other: def extract_third_column(row): ... return row[2] ... rows.sort(key=extract_third_column) pprint(rows) [['X ', ' 88115.39', ' 0.00', ''], ['X ', ' 88335.39', ' 0.00', ''], ['X ', ' 90443.29', ' 0.00', ''], ['X ', ' 90453.29', ' 0.00', ''], ['X ', ' 90453.29', ' 0.00', ''], ['c ', ' 1.11', ' 0.00', ''], ['d ', ' 1.11', ' 0.00', ''], ['a ', ' 0.00', ' 1.11', ''], ['b ', ' 0.00', ' 1.11', ''], ['X ', ' 0.00', ' 88115.39', ''], ['X ', ' 0.00', ' 88335.39', ''], ['X ', ' 0.00', ' 90443.29', '']] The key function you actually need is a bit more sophisticated. You want rows with equal nonzero values to end close together, no matter whether the interesting value is in the second or third column. Let's try: def extract_nonzero_column(row): ... if row[1] == ' 0.00': ... return row[2] ... else: ... return row[1] ... Here's a preview of the keys: for row in rows: ... print
Re: [Tutor] compare and arrange file
Edgar Almonte wrote: thanks emile i understand exactly what you explain me but i was unable to accomplish it ( too noob in python ) but i solved the problem with this code http://pastebin.com/4A6Jz4wZ i will try do what you suggest me anyway but that will tomorrow , tonight i done and i feel good :D When you're done compare it to the one below: import csv def sortkey(row): if float(row[1]): return row[1], True else: return row[2], False with open(infile.txt, rb) as instream: rows = sorted(csv.reader(instream, delimiter=|), key=sortkey) with open(outfile.txt, wb) as outstream: csv.writer(outstream, delimiter=|).writerows(rows) ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] compare and arrange file
On 07/12/2011 12:56 AM, Edgar Almonte wrote: thanks emile i understand exactly what you explain me but i was unable to accomplish it ( too noob in python ) but i solved the problem with this code http://pastebin.com/4A6Jz4wZ (When you post on this list, your comments should follow the pieces you're responding to. That's a long-standing convention.) As I explained earlier, your nested loops won't work correctly, as you fail to re-open the orig1 file. Since you still seem to think it'll work, I'll just hope this assignment is for fun, and not for anything that matters. In particular, the first line will be compared to all the others. But if the third line should have matched the seventh, it won't get written out. -- DaveA ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] compare and arrange file
On Tue, Jul 12, 2011 at 5:44 AM, Peter Otten __pete...@web.de wrote: Edgar Almonte wrote: thanks emile i understand exactly what you explain me but i was unable to accomplish it ( too noob in python ) but i solved the problem with this code http://pastebin.com/4A6Jz4wZ i will try do what you suggest me anyway but that will tomorrow , tonight i done and i feel good :D When you're done compare it to the one below: import csv def sortkey(row): if float(row[1]): return row[1], True else: return row[2], False with open(infile.txt, rb) as instream: rows = sorted(csv.reader(instream, delimiter=|), key=sortkey) with open(outfile.txt, wb) as outstream: csv.writer(outstream, delimiter=|).writerows(rows) ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor This look aweasome i will try it later , thanks ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] compare and arrange file
On Tue, Jul 12, 2011 at 7:28 AM, Dave Angel d...@davea.name wrote: On 07/12/2011 12:56 AM, Edgar Almonte wrote: thanks emile i understand exactly what you explain me but i was unable to accomplish it ( too noob in python ) but i solved the problem with this code http://pastebin.com/4A6Jz4wZ (When you post on this list, your comments should follow the pieces you're responding to. That's a long-standing convention.) As I explained earlier, your nested loops won't work correctly, as you fail to re-open the orig1 file. Since you still seem to think it'll work, I'll just hope this assignment is for fun, and not for anything that matters. In particular, the first line will be compared to all the others. But if the third line should have matched the seventh, it won't get written out. -- DaveA hmm i still don't get you point , i mean why you say that will fail , i copy/read the file 2 times and compare line by line with the second copy of the file ( check the new code at top i do a read() for full read it not more online reading ). anyway i will do the case that you explain and look the result. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] compare and arrange file
On 07/12/2011 08:25 AM, Edgar Almonte wrote: On Tue, Jul 12, 2011 at 7:28 AM, Dave Angeld...@davea.name wrote: On 07/12/2011 12:56 AM, Edgar Almonte wrote: thanks emile i understand exactly what you explain me but i was unable to accomplish it ( too noob in python ) but i solved the problem with this code http://pastebin.com/4A6Jz4wZ (When you post on this list, your comments should follow the pieces you're responding to. That's a long-standing convention.) As I explained earlier, your nested loops won't work correctly, as you fail to re-open the orig1 file. Since you still seem to think it'll work, I'll just hope this assignment is for fun, and not for anything that matters. In particular, the first line will be compared to all the others. But if the third line should have matched the seventh, it won't get written out. -- DaveA hmm i still don't get you point , i mean why you say that will fail , i copy/read the file 2 times and compare line by line with the second copy of the file ( check the new code at top i do a read() for full read it not more online reading ). anyway i will do the case that you explain and look the result. You're absolutely right. I missed the fact that you did a read() and split() at the top. As long as you have enough memory to hold two copies of the file, that's great. DaveA ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] compare and arrange file
On Tue, Jul 12, 2011 at 5:44 AM, Peter Otten __pete...@web.de wrote: Edgar Almonte wrote: thanks emile i understand exactly what you explain me but i was unable to accomplish it ( too noob in python ) but i solved the problem with this code http://pastebin.com/4A6Jz4wZ i will try do what you suggest me anyway but that will tomorrow , tonight i done and i feel good :D When you're done compare it to the one below: import csv def sortkey(row): if float(row[1]): return row[1], True else: return row[2], False with open(infile.txt, rb) as instream: rows = sorted(csv.reader(instream, delimiter=|), key=sortkey) with open(outfile.txt, wb) as outstream: csv.writer(outstream, delimiter=|).writerows(rows) ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor That code work flawless , aweasome , can you explain to me the code, i have a idea but if you don't mind can you ? ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] compare and arrange file
On 7/12/2011 4:01 PM Edgar Almonte said... On Tue, Jul 12, 2011 at 5:44 AM, Peter Otten__pete...@web.de wrote: snip import csv imports the comma separated values (csv) file handler utilities module def sortkey(row): if float(row[1]): return row[1], True else: return row[2], False ... sortkey defines a function that accepts a cvs.reader data row, and returns either row[1] and True or row[2] and False based on which of row[1] and row[2] has a non-zero value with open(infile.txt, rb) as instream: rows = sorted(csv.reader(instream, delimiter=|), key=sortkey) rows becomes the sortkey sorted result of the lines of infile.txt with open(outfile.txt, wb) as outstream: csv.writer(outstream, delimiter=|).writerows(rows) ... and this writes those results to outfile.txt you might also try the shortened: from csv import reader,writer def sortkey(row): return max(row[1],row[2]),row[1]row[2] writer(open(outfile.txt, wb), delimiter=|).writerows( sorted(reader(open(infile.txt, rb), delimiter=|),key=sortkey)) Emile ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] compare and arrange file
hello , i have a file this a structure like this X | 0.00| 88115.39| X | 90453.29| 0.00| X | 0.00| 90443.29| X | 88115.39| 0.00| X | 0.00| 88335.39| X | 90453.29| 0.00| X | 88335.39| 0.00| X | 90443.29| 0.00| now i need re-arrange the file in this way: X | 0.00| 88115.39| X | 88115.39| 0.00| X | 0.00| 90453.29| X | 90453.29| 0.00| etc i try this http://pastebin.com/2mvxn5GY but without look maybe somebody can give some hint , i know that this is maybe not a directly python question but if somebody can help me I will appreciate it Thanks ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] compare and arrange file
On 7/11/2011 3:16 PM Edgar Almonte said... hello , i have a file this a structure like this X | 0.00| 88115.39| X | 90453.29| 0.00| X | 0.00| 90443.29| X | 88115.39| 0.00| X | 0.00| 88335.39| X | 90453.29| 0.00| X | 88335.39| 0.00| X | 90443.29| 0.00| now i need re-arrange the file in this way: X | 0.00| 88115.39| X | 88115.39| 0.00| X | 0.00| 90453.29| X | 90453.29| 0.00| etc It's not obvious to me for your sample what you want. For example, the 2nd value 90453.29 from the re-arranged group doesn't appear in the top sample. If I venture a guess, it seems to me that you want the debits and corresponding offsetting credits listed in sequence. In pseudo-code, that might me done as: read lines from file for each line in lines set flag to D or C based on values set sortkey to value+flag append sortkey and line to decorated list sort decorated list for key,line in decorated list print line HTH, Emile i try this http://pastebin.com/2mvxn5GY but without look maybe somebody can give some hint , i know that this is maybe not a directly python question but if somebody can help me I will appreciate it Thanks ___ 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] compare and arrange file
On 07/11/2011 06:39 PM, Emile van Sebille wrote: On 7/11/2011 3:16 PM Edgar Almonte said... hello , i have a file this a structure like this X | 0.00| 88115.39| X | 90453.29| 0.00| X | 0.00| 90443.29| X | 88115.39| 0.00| X | 0.00| 88335.39| X | 90453.29| 0.00| X | 88335.39| 0.00| X | 90443.29| 0.00| now i need re-arrange the file in this way: X | 0.00| 88115.39| X | 88115.39| 0.00| X | 0.00| 90453.29| X | 90453.29| 0.00| etc It's not obvious to me for your sample what you want. For example, the 2nd value 90453.29 from the re-arranged group doesn't appear in the top sample. If I venture a guess, it seems to me that you want the debits and corresponding offsetting credits listed in sequence. In pseudo-code, that might me done as: read lines from file for each line in lines set flag to D or C based on values set sortkey to value+flag append sortkey and line to decorated list sort decorated list for key,line in decorated list print line HTH, Emile i try this http://pastebin.com/2mvxn5GY but without look I also can't see any pattern in the data to give a clue what kind of filtering you're trying to do. A more specific spec would be useful. I can comment on your pastebin code, however. You should have pasted it in your message, since it's short. 1. def splitline(line, z): 2. if z == 0: 3. pass 4. 5. fields = line.split('|') 6. nlist = [] 7. for field in fields: 8. nlist.append(field) 9. 10. return nlist[z] The whole loop with nlist is a waste of energy, as you already got a list from split(). You could replace the function with def splitline(line, z): return line.split('|')[z] The if z==0 doesn't do anything either. Perhaps you meant to do some error checking in case the line doesn't have at least z fields. But the real problem in your code is the you posted for loop. The inner loop takes multiple passes through the orig1 file, but the second time won't get anything, since the file is already positioned at the end. I'd simply move the open statement inside the outer loop. There may be other problems, but that could get you going. DaveA -- DaveA ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] compare and arrange file
Thanks for the hints , what i want accomplish is sort the line by the same value in the column 2 and 3 i mean the line with the same value in the 2 get together with the line in the same value in column 3 emile and david thanks again let me check the hint that your give me, i will feedback the code if everything workout or else :D On Mon, Jul 11, 2011 at 7:35 PM, Dave Angel d...@davea.name wrote: On 07/11/2011 06:39 PM, Emile van Sebille wrote: On 7/11/2011 3:16 PM Edgar Almonte said... hello , i have a file this a structure like this X | 0.00| 88115.39| X | 90453.29| 0.00| X | 0.00| 90443.29| X | 88115.39| 0.00| X | 0.00| 88335.39| X | 90453.29| 0.00| X | 88335.39| 0.00| X | 90443.29| 0.00| now i need re-arrange the file in this way: X | 0.00| 88115.39| X | 88115.39| 0.00| X | 0.00| 90453.29| X | 90453.29| 0.00| etc It's not obvious to me for your sample what you want. For example, the 2nd value 90453.29 from the re-arranged group doesn't appear in the top sample. If I venture a guess, it seems to me that you want the debits and corresponding offsetting credits listed in sequence. In pseudo-code, that might me done as: read lines from file for each line in lines set flag to D or C based on values set sortkey to value+flag append sortkey and line to decorated list sort decorated list for key,line in decorated list print line HTH, Emile i try this http://pastebin.com/2mvxn5GY but without look I also can't see any pattern in the data to give a clue what kind of filtering you're trying to do. A more specific spec would be useful. I can comment on your pastebin code, however. You should have pasted it in your message, since it's short. def splitline(line, z): if z == 0: pass fields = line.split('|') nlist = [] for field in fields: nlist.append(field) return nlist[z] The whole loop with nlist is a waste of energy, as you already got a list from split(). You could replace the function with def splitline(line, z): return line.split('|')[z] The if z==0 doesn't do anything either. Perhaps you meant to do some error checking in case the line doesn't have at least z fields. But the real problem in your code is the you posted for loop. The inner loop takes multiple passes through the orig1 file, but the second time won't get anything, since the file is already positioned at the end. I'd simply move the open statement inside the outer loop. There may be other problems, but that could get you going. DaveA -- DaveA ___ 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] compare and arrange file
back again, yes that is the idea: If I venture a guess, it seems to me that you want the debits and corresponding offsetting credits listed in sequence. but not sure if i get you pseudo code , you mean some how flag the line when is D or C ( credit of debit ) and then sort by what ? On Mon, Jul 11, 2011 at 6:39 PM, Emile van Sebille em...@fenx.com wrote: On 7/11/2011 3:16 PM Edgar Almonte said... hello , i have a file this a structure like this X | 0.00| 88115.39| X | 90453.29| 0.00| X | 0.00| 90443.29| X | 88115.39| 0.00| X | 0.00| 88335.39| X | 90453.29| 0.00| X | 88335.39| 0.00| X | 90443.29| 0.00| now i need re-arrange the file in this way: X | 0.00| 88115.39| X | 88115.39| 0.00| X | 0.00| 90453.29| X | 90453.29| 0.00| etc It's not obvious to me for your sample what you want. For example, the 2nd value 90453.29 from the re-arranged group doesn't appear in the top sample. offsetting credits listed in sequence In pseudo-code, that might me done as: read lines from file for each line in lines set flag to D or C based on values set sortkey to value+flag append sortkey and line to decorated list sort decorated list for key,line in decorated list print line HTH, Emile i try this http://pastebin.com/2mvxn5GY but without look maybe somebody can give some hint , i know that this is maybe not a directly python question but if somebody can help me I will appreciate it Thanks ___ 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 maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] compare and arrange file
back again david i do the for because the line is delimited by pipeline so and i need get the field number 2 and 3 of the line if i understand well the split('|')[z] will just split till there ( z value ) so if i do split('|')[2] i will get: X , 0.00 and i just want the number value part of the line i try putting the read of orig1 inside the fist loop but that don't see work because the thing is that for some reason the fist loop is just passing one time ( the first one if you see in my code i put a print line1 in the first loop and a print value in the second one and i get something line line1--- value1 value1 value1 value1 value1 value1 etc. pd: sorry for my bad english On Mon, Jul 11, 2011 at 7:35 PM, Dave Angel d...@davea.name wrote: On 07/11/2011 06:39 PM, Emile van Sebille wrote: On 7/11/2011 3:16 PM Edgar Almonte said... hello , i have a file this a structure like this X | 0.00| 88115.39| X | 90453.29| 0.00| X | 0.00| 90443.29| X | 88115.39| 0.00| X | 0.00| 88335.39| X | 90453.29| 0.00| X | 88335.39| 0.00| X | 90443.29| 0.00| now i need re-arrange the file in this way: X | 0.00| 88115.39| X | 88115.39| 0.00| X | 0.00| 90453.29| X | 90453.29| 0.00| etc It's not obvious to me for your sample what you want. For example, the 2nd value 90453.29 from the re-arranged group doesn't appear in the top sample. If I venture a guess, it seems to me that you want the debits and corresponding offsetting credits listed in sequence. In pseudo-code, that might me done as: read lines from file for each line in lines set flag to D or C based on values set sortkey to value+flag append sortkey and line to decorated list sort decorated list for key,line in decorated list print line HTH, Emile i try this http://pastebin.com/2mvxn5GY but without look I also can't see any pattern in the data to give a clue what kind of filtering you're trying to do. A more specific spec would be useful. I can comment on your pastebin code, however. You should have pasted it in your message, since it's short. def splitline(line, z): if z == 0: pass fields = line.split('|') nlist = [] for field in fields: nlist.append(field) return nlist[z] The whole loop with nlist is a waste of energy, as you already got a list from split(). You could replace the function with def splitline(line, z): return line.split('|')[z] The if z==0 doesn't do anything either. Perhaps you meant to do some error checking in case the line doesn't have at least z fields. But the real problem in your code is the you posted for loop. The inner loop takes multiple passes through the orig1 file, but the second time won't get anything, since the file is already positioned at the end. I'd simply move the open statement inside the outer loop. There may be other problems, but that could get you going. DaveA -- DaveA ___ 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] compare and arrange file
On 11-Jul-11 16:50, Edgar Almonte wrote: Thanks for the hints , what i want accomplish is sort the line by the same value in the column 2 and 3 i mean the line with the same value in the 2 get together with the line in the same value in column 3 What if the same value appears more than once? Does it matter which ones you match up? If so, how do you decide? -- Steve Willoughby / st...@alchemy.com A ship in harbor is safe, but that is not what ships are built for. PGP Fingerprint 4615 3CCE 0F29 AE6C 8FF4 CA01 73FE 997A 765D 696C ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] compare and arrange file
this is just one time thing and the value don't get repeat On Mon, Jul 11, 2011 at 7:55 PM, Steve Willoughby st...@alchemy.com wrote: On 11-Jul-11 16:50, Edgar Almonte wrote: Thanks for the hints , what i want accomplish is sort the line by the same value in the column 2 and 3 i mean the line with the same value in the 2 get together with the line in the same value in column 3 What if the same value appears more than once? Does it matter which ones you match up? If so, how do you decide? -- Steve Willoughby / st...@alchemy.com A ship in harbor is safe, but that is not what ships are built for. PGP Fingerprint 4615 3CCE 0F29 AE6C 8FF4 CA01 73FE 997A 765D 696C ___ 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] compare and arrange file
On 11-Jul-11 17:18, Edgar Almonte wrote: this is just one time thing and the value don't get repeat Then you could make a single loop over the input lines, building two dictionaries as you go: * one that maps column 2's value to the rest of that line's data * and one that does this for column 3's value. Now run through the column 2 data you saved, print that data row, then look up the value in the other dictionary and print that after it. On Mon, Jul 11, 2011 at 7:55 PM, Steve Willoughbyst...@alchemy.com wrote: On 11-Jul-11 16:50, Edgar Almonte wrote: Thanks for the hints , what i want accomplish is sort the line by the same value in the column 2 and 3 i mean the line with the same value in the 2 get together with the line in the same value in column 3 What if the same value appears more than once? Does it matter which ones you match up? If so, how do you decide? -- Steve Willoughby / st...@alchemy.com A ship in harbor is safe, but that is not what ships are built for. PGP Fingerprint 4615 3CCE 0F29 AE6C 8FF4 CA01 73FE 997A 765D 696C ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor -- Steve Willoughby / st...@alchemy.com A ship in harbor is safe, but that is not what ships are built for. PGP Fingerprint 4615 3CCE 0F29 AE6C 8FF4 CA01 73FE 997A 765D 696C ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] compare and arrange file
i not too smart steve , can you show me with code ? On Mon, Jul 11, 2011 at 8:22 PM, Steve Willoughby st...@alchemy.com wrote: On 11-Jul-11 17:18, Edgar Almonte wrote: this is just one time thing and the value don't get repeat Then you could make a single loop over the input lines, building two dictionaries as you go: * one that maps column 2's value to the rest of that line's data * and one that does this for column 3's value. Now run through the column 2 data you saved, print that data row, then look up the value in the other dictionary and print that after it. On Mon, Jul 11, 2011 at 7:55 PM, Steve Willoughbyst...@alchemy.com wrote: On 11-Jul-11 16:50, Edgar Almonte wrote: Thanks for the hints , what i want accomplish is sort the line by the same value in the column 2 and 3 i mean the line with the same value in the 2 get together with the line in the same value in column 3 What if the same value appears more than once? Does it matter which ones you match up? If so, how do you decide? -- Steve Willoughby / st...@alchemy.com A ship in harbor is safe, but that is not what ships are built for. PGP Fingerprint 4615 3CCE 0F29 AE6C 8FF4 CA01 73FE 997A 765D 696C ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor -- Steve Willoughby / st...@alchemy.com A ship in harbor is safe, but that is not what ships are built for. PGP Fingerprint 4615 3CCE 0F29 AE6C 8FF4 CA01 73FE 997A 765D 696C ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] compare and arrange file
On 7/11/2011 5:02 PM Edgar Almonte said... back again, yes that is the idea: If I venture a guess, it seems to me that you want the debits and corresponding offsetting credits listed in sequence. but not sure if i get you pseudo code , you mean some how flag the line when is D or C ( credit of debit ) and then sort by what ? When you sort a list of tuple pairs, it sort on the first item, so set sortkey to value+flag means the first tuple in the list of tuples to sort should end up with as value+flag, where the flag is set based on the non-zero value in your line. For example, XXXs,Dval,Cval = line.split(|) then, assuming a consistent valid file structure, if int(Dval): key=D+Dval else: key=C+Cval then, append (key,line) to your decorated list for each line, and finally sort and print the lines from your decorated list. Emile hello , i have a file this a structure like this X | 0.00| 88115.39| X | 90453.29| 0.00| snip In pseudo-code, that might me done as: read lines from file for each line in lines set flag to D or C based on values set sortkey to value+flag append sortkey and line to decorated list sort decorated list for key,line in decorated list print line ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] compare and arrange file
thanks emile i understand exactly what you explain me but i was unable to accomplish it ( too noob in python ) but i solved the problem with this code http://pastebin.com/4A6Jz4wZ i will try do what you suggest me anyway but that will tomorrow , tonight i done and i feel good :D Thanks all for the help On Mon, Jul 11, 2011 at 11:01 PM, Emile van Sebille em...@fenx.com wrote: On 7/11/2011 5:02 PM Edgar Almonte said... back again, yes that is the idea: If I venture a guess, it seems to me that you want the debits and corresponding offsetting credits listed in sequence. but not sure if i get you pseudo code , you mean some how flag the line when is D or C ( credit of debit ) and then sort by what ? When you sort a list of tuple pairs, it sort on the first item, so set sortkey to value+flag means the first tuple in the list of tuples to sort should end up with as value+flag, where the flag is set based on the non-zero value in your line. For example, XXXs,Dval,Cval = line.split(|) then, assuming a consistent valid file structure, if int(Dval): key=D+Dval else: key=C+Cval then, append (key,line) to your decorated list for each line, and finally sort and print the lines from your decorated list. Emile hello , i have a file this a structure like this X | 0.00| 88115.39| X | 90453.29| 0.00| snip In pseudo-code, that might me done as: read lines from file for each line in lines set flag to D or C based on values set sortkey to value+flag append sortkey and line to decorated list sort decorated list for key,line in decorated list print line ___ 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