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

    Join Date
    Dec 2012
    Posts
    1
    Rep Power
    0

    Read Data From File And Analyse


    I have a text file that has data like the following:

    20121204 983231
    20121205 983122
    20121206 973124


    The first column is the date, second is a user ID. I want to load this text file into Python and analyse it. What is the best way to store it? An array or table/list?

    Sort of analysis I'd do is what user ID was date stamped 5 days ago.

    This is where I am right now:

    Code:
    #!/usr/bin/env python
    
    f = open('data.txt','r+')
    
    data_list = f.readlines()
    
    f.close()
    When I print data_list, I get the data, but it also has the MS-DOS /n new line character too. Ideally, I'd want to store the date along with the user ID in an array where I can query them.
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Location
    Iran
    Posts
    149
    Rep Power
    140
    I would rather use a dictionary for that.

    Assuming that the structure of the file is like what you mentioned, that is two columns, respectively: stamp date and userid, the following can be used

    Code:
    lineNumber = 0
    dictionary = {}
    with open("./data.txt") as usersFile:
        for line in usersFile:
            dataList = line.split()
            dictionary[dataList[1]] = dataList[0]
            
    userId = raw_input("Enter a userId: ")
    print("The user stamped at: {stampDate}".
        format(stampDate=str(dictionary[userId])))

    Regards,
    Dariyoosh

IMN logo majestic logo threadwatch logo seochat tools logo