[Baypiggies] newbie question - automating my reporting tasks

Max Slimmer max at theslimmers.net
Thu May 17 01:15:39 CEST 2007


def getData():
    f = open('datafile.txt')
    itmList = []
     for line in f:
        itm = MYCLASS()
        itm.name = line[0:20].strip()  # get name from 1st 20 chars in line
and strip any trailing (in this case also leading vs rstrip()) whitespace
        itm.rate = float(line[20:28]   
        itmList.append(itm)
    f.close()
 
# now you can iterate over itmList you could also have just made a list of
lists instead of a list of MYCLASS objects
 
class MYCLASS(object):
    pass      # this creates an empty object and you can dynamically add
members to it.
 
max


  _____  

From: Alden Meneses [mailto:aldenm at gmail.com] 
Sent: Wednesday, May 16, 2007 3:25 PM
To: Max Slimmer
Cc: baypiggies at python.org
Subject: Re: [Baypiggies] newbie question - automating my reporting tasks


Max - Thanks for your reply. The text files are not in csv format and I will
need to figure out how to separate the fields. Space as a separator will not
work as values have spaces in them. The current text file I am looking at
seems to have fields that are at fixed width and the each line I want are
encapsulated in "". The example you showed below will be usefull once I
accomplish the above task. 
 
Chris - Thank you for your reply. The link you provided have some examples
of file and string manipulation which will be a very useful read.
 
Alden

 
On 5/16/07, Max Slimmer <max at theslimmers.net > wrote: 

You indicated you receive text files, if they are comma delimited you can
parse them with the csv library and thus read them into python lists of
lists 
onece you have your data in a list of lists you could simply operate on it
there or you could create objects from each list element leaving you with a
list of objects exch representing one row. 
 
say you keep it simple and you have incoming data consisting of name, rate,
amt. 
>>> data = [['name1',12.5, 123.66],['name2',5.6,421]]
>>> data
[['name1', 12.5, 123.66], ['name2', 5.5999999999999996, 421]]
you can sum all the amt's with 
>>> tot = sum(row[2] for row in data)
>>> tot
544.65999999999997
or you can sort the data by rate:
>>> data.sort(key=lambda i:i[1])   # see
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/305304 for
interesting things to do with sort
>>> data
[['name2', 5.5999999999999996, 421], ['name1', 12.5, 123.66]]
 
play with the interactive interpreter 
 
max
 


  _____  

From: baypiggies-bounces at python.org [mailto:
<mailto:baypiggies-bounces at python.org> baypiggies-bounces at python.org] On
Behalf Of Alden Meneses
Sent: Wednesday, May 16, 2007 10:45 AM
To: baypiggies at python.org
Subject: [Baypiggies] newbie question - automating my reporting tasks

 

Hello all,
 
It does seem that Python is easier to lean than PERL. and I like the
interpreter.
 
I have some text files that I download from an AS400 and format with Excel.
Basically I convert the text to columns then sort and subtotal. The results
are then emailed to various people and also used in other reports. Haven't
really thought out how to automate this whole process but wanted to start
with a project to learn python and go from there. 
 
I have skimmed throught the python tutorial and now getting into python in a
nutshell, 2nd edition and would welcome any thoughts or suggestions in
getting my project off the ground. I've taken programming courses in college
- C, C++, PERL, FORTRAN, PASCAL, UNIX shell scripting and its been awhile
since I had to rely on it. 
 
Thanks in advance,
Alden


-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/baypiggies/attachments/20070516/0cc0ca2f/attachment.html 


More information about the Baypiggies mailing list