# [Tutor] Compute data usage from log

Christian Witts cwitts at compuscan.co.za
Mon Oct 26 09:20:58 CET 2009

```bibi midi wrote:
> Hi all!
>
> I have a file with data structure derived from wvdial log:
> Oct 14 11:03:45 cc000002695 pppd[3092]: Sent 3489538 bytes, received
> 43317854 bytes.
>
> I want to get the 10th field of each line and get the sum for all
> lines (total my net data usage). In awk u can easily pop it using
> field variables e..g \$10. Since im learning python on my own i thought
> this is a good way to learn but im stumped how to achieve this goal.
> I used open() and read line method to store the contents in a list but
> it is a list of string. I need it to be numbers. Iterating through the
> string list definitely is unwieldy. Can you guys advise best way how?
>
> FWIW im a self learner and just relying from tutorial materials and
> this medium to learn python. Lead me to the right direction please :-)
>
> My IDE is vim and ipython in debian linux.
>
>
> --
> Best Regards,
> bibimidi
>
>
> ------------------------------------------------------------------------
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
fInput = open('/path/to/log.file', 'rb')
total_usage = 0
for line in fInput:
total_usage += int(line.split(' ')[9].strip())
print total_usage

That would be the simple no frills version.  What it does is iterate
through the file, on each line it splits it into columns delimited by
spaces, takes the 10th element (you count from 0 up and not 1 up),
converts it into an integer and adds it to your total_usage counter.

Of course this has no error checking and or niceties, but I will leave
that up to you.
Hope that helps.

--
Kind Regards,
Christian Witts

```