[Tutor] please help

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Mon Jul 11 06:06:14 CEST 2005



On Sun, 10 Jul 2005, Srinivas Iyyer wrote:

> I have a file that looks like this:
>
> //
> AB32456\tTransaction from India
>         \t 43 \t 34
>         \t 34 \t 65
>         \t 12 \t 35
> //

[some lines cut]


> What I have to do:
> //
> AB32456\tTransaction from India
> AB32456\t 43 \t 34
> AB32456\t 34 \t 65
> AB32456\t 12 \t 35

[some lines cut]


Ok, so it looks like we want to then remember the first column at the
beginning of a transaction, and then have duplicate that value for the
rest of the transaction.  Let's look at what you have so far.

> f1 = open('myfile','r')
> stuff = f1.read().split('\n')
> for i in stuff:
>    if i != '//':
>         trcode = line.split('\t')[0]
>         trquant = line.split('\t')[1]


It might help to simplify the problem a little.  I think you might be
getting stuck because you're trying to handle the '\\' end of a
transaction as well as the column value-trickling stuff, so there's a bit
of mixing going on.


One way to simplifying a problem is to concentrate on handling a single
thing.  Let's say that we have a list of lines from a single transaction:

######
sample_data = ["AB32456\tTransaction from India",
               "        \t 43 \t 34",
               "        \t 34 \t 65",
               "        \t 12 \t 35"]
######

Can you transform these lines so that the AB32456 value trickles into the
other lines of the transaction?  Don't worry about "//" stuff at all in
this subproblem.

Try handling this first: it's the core of your program anyway, so might as
well figure that part out.  *grin*


If you have more questions, please feel free to ask.  Best of wishes to
you!



More information about the Tutor mailing list