Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Sorting files into various orders

stustu Posts: 1Member
Hi,
Im fairly new to Python, and have several textbooks on the language (Programming Python and Learning Python) but I still need a little help...

Im writing a program that will take a long IP log file, and sort it out into a list depending on user set variables, such as sort the list by Protocol type and sort the list by IP address (to name but two).

Im thinking of using a list, but am unsure how to load the log file into the list, and the best way to index it.. any help would be very much appreciated!

Thanks

Comments

  • infidelinfidel Posts: 2,900Member
    : Hi,
    : Im fairly new to Python, and have several textbooks on the language (Programming Python and Learning Python) but I still need a little help...
    :
    : Im writing a program that will take a long IP log file, and sort it out into a list depending on user set variables, such as sort the list by Protocol type and sort the list by IP address (to name but two).
    :
    : Im thinking of using a list, but am unsure how to load the log file into the list, and the best way to index it.. any help would be very much appreciated!

    Loading a file into a list is fairly trivial.

    [code]
    lines = open(filename,'r').read().split('
    ')
    [/code]

    That will give you a list of the lines in the file (minus the newline characters). You can also do it like this:

    [code]
    lines = open(filename,'r').readlines()
    [/code]

    However, that leaves the newline characters at the end of each list element and I don't personally care for that.

    Now, if you know how each line can be split up into fields, you can make a list of tuples:

    [code]
    records = []
    for line in lines:
    record = (line[0:50], line[51-100]) # split line into a tuple of fields
    records.append(record)
    [/code]

    If that worked right, you should end up with a list where each element is a tuple containing the fields from one line of your log file.

    Once you have the data parsed like this, you can pick any sort algorithm you like. Bubblesort is fairly easy. Not as efficient perhaps, but easy.


    [size=5][italic][blue][RED]i[/RED]nfidel[/blue][/italic][/size]

Sign In or Register to comment.