#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    2
    Rep Power
    0

    Python Script to Generate a Field Based on Parameterized Calculation


    I am pretty new to python and got an interesting request. I have a result set that is structured as such but on a much larger scale and includes multiple distinct acct. no.'s

    acct no. date Event
    11111111 2012/01/01 1
    11111111 2012/01/05 1

    I'm going to be attaching a secondary date object that will transform the result set into this output:

    acct no. date Event
    11111111 2012/01/01 1
    11111111 2012/01/02 0
    11111111 2012/01/03 0
    11111111 2012/01/04 0
    11111111 2012/01/05 1
    11111111 2012/01/06 0

    Heres the Request:

    I've been asked to build a script that measures the distance between two dates, calculates the difference, in number of days and generate a flag if it falls in that distance. The tricky part is when there is a new record set I need to append the results of the first record set onto and move onto the next and continue the calculation and flag generation.

    The final output should look something like this if the threshold is defined as 1 day:

    acct no. date Event Recent
    11111111 2012/01/01 1 Y
    11111111 2012/01/02 0 Y
    11111111 2012/01/03 0 N
    11111111 2012/01/04 0 N
    11111111 2012/01/05 1 Y
    11111111 2012/01/06 0 Y


    I'm still relatively green in python and cant think of where to start.

    Deeply appreciate any help.

    Thanks,

    Joe
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    2
    Rep Power
    0
    Any help would be greatly appreciated.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2010
    Posts
    153
    Rep Power
    5
    Depending on the size of the data set, it's probably easiest to pull the data into a list and work with it all at once. If the data set is large, that's probably not going to scale though.

    Here's a few pointers:

    - You can probably parse the dates using strptime() from the datetime module. Once you've got the date as a datetime, figuring out the distance between dates, sorting dates, etc. will be much easier.

    - Don't look at this as rewriting the file in-place. You are reading in one file and writing out another. This makes things a lot simpler.

    - Basically, you want to setup up some flags, then start looping through the source data, check the flag values, write the output accordingly, and set the flag values for the next iteration.

    Maybe that helps you, hopefully so.

IMN logo majestic logo threadwatch logo seochat tools logo