[Tutor] compare and arrange file
Dave Angel
d at davea.name
Tue Jul 12 01:35:34 CEST 2011
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
>> XXXXXXXXX XXXX| 0000000000000.00| 0000000088115.39|
>> XXXXXXXXX XXXX| 0000000090453.29| 0000000000000.00|
>> XXXXXXXXX XXXX| 0000000000000.00| 0000000090443.29|
>> XXXXXXXXX XXXX| 0000000088115.39| 0000000000000.00|
>> XXXXXXXXX XXXX| 0000000000000.00| 0000000088335.39|
>> XXXXXXXXX XXXX| 0000000090453.29| 0000000000000.00|
>> XXXXXXXXX XXXX| 0000000088335.39| 0000000000000.00|
>> XXXXXXXXX XXXX| 0000000090443.29| 0000000000000.00|
>>
>> now i need re-arrange the file in this way:
>> XXXXXXXXX XXXX| 0000000000000.00| 0000000088115.39|
>> XXXXXXXXX XXXX| 0000000088115.39| 0000000000000.00|
>> XXXXXXXXX XXXX| 0000000000000.00| 0000000090453.29|
>> XXXXXXXXX XXXX| 0000000090453.29| 0000000000000.00|
>>
>> etc....
>
> It's not obvious to me for your sample what you want. For example,
> the 2nd value 0000000090453.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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110711/d0daf727/attachment.html>
More information about the Tutor
mailing list