Thread: File Sorting

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

    Join Date
    Apr 2013
    Posts
    16
    Rep Power
    0

    File Sorting


    Hi there, I have recently started to learn python and im looking for some help. I have a text file that i am trying to read and sort. I dont know how to read the file, i have been using the file.read method but i doesnt seem to work? the code below is what i have so far but i cant get the program to print out the file. Also any advice on sorting the file would be greatly appreciated!
    Thanks
    Code:
    #open a file
    
    file = open("datatest.txt","r")
    print ("Name of the file: ", file.name)
    
    #reads character
    print (file.read(1))
    print (file.read(2))
    
    
    #reads the whole file
    print (file.read())
    
    
    #closing the file
    
    file.close()
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Location
    /dev/null
    Posts
    164
    Rep Power
    19
    Not an efficient method, but this will work for small sized files.

    Code:
    f = open ('/path/to/file', 'r')
    lines = [l.rstrip() for l in f]
    lines.sort()
    f.close()
    
    for line in lines:
        print(line)
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    16
    Rep Power
    0
    Originally Posted by noobie1000
    Not an efficient method, but this will work for small sized files.

    Code:
    f = open ('/path/to/file', 'r')
    lines = [l.rstrip() for l in f]
    lines.sort()
    f.close()
    
    for line in lines:
        print(line)
    Cool thank you very much! Say the file consisted of records like the example below and i wanted to sort all the files by the last element (ie >50k or <=50k) what would i put into the line.sort? sorry for the silly question, still trying to get my head around python

    53, Private, 234721, 11th, 7, Married-civ-spouse, Handlers-cleaners, Husband, Black, Male, 0, 0, 40, United-States, <=50K
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,996
    Rep Power
    481

    An idea


    def sort_key_computation_function(LINE):
    return LINE.split()[-1] # return the last "word" from LINE

    data.sort(key=sort_key_computation_function)




    # untested. My untested codes almost always fail.
    [code]Code tags[/code] are essential for python code and Makefiles!
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    16
    Rep Power
    0
    Originally Posted by b49P23TIvg
    def sort_key_computation_function(LINE):
    return LINE.split()[-1] # return the last "word" from LINE

    data.sort(key=sort_key_computation_function)




    # untested. My untested codes almost always fail.
    will this not just return the 'word'? cause i want to return the whole thing again not just a little part of it?
  10. #6
  11. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,996
    Rep Power
    481
    Hmm, my response from 14 hours ago was lost. Probably because it was nasty.

    Way to try my suggestion, dude/dudette.
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo