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

    Join Date
    Aug 2013
    Posts
    3
    Rep Power
    0

    Sorting text in the file in alphabetical order


    Hi,

    pls assist how do i sort the contents of the file in alphabetical order getting the below error

    AttributeError: 'str' object has no attribute 'sort'

    here is my code
    #!/usr/bin/python

    # Open a file
    fileName = input("Enter the file Name:")
    inputFile = open("C:\TextIntofile.txt", 'r')
    text = inputFile.read()
    text.sort()
    print('The input in alphabetical order below : \n', text)



    #fileName.write( "Python is not a cool language")

    # Close opend file
    #fileName.close()
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2007
    Location
    Joensuu, Finland
    Posts
    428
    Rep Power
    66
    Originally Posted by tools[B
    AttributeError: 'str' object has no attribute 'sort'[/B]
    True, str objects donít know how to sort(). In IDLE:

    Code:
    >>> text = 'first line\nsecond line\nthird line\nfourth line'
    >>> print(text)
    first line
    second line
    third line
    fourth line
    >>> text.sort()
    Traceback (most recent call last):
      File "<pyshell#2>", line 1, in <module>
        text.sort()
    AttributeError: 'str' object has no attribute 'sort'
    However, list objects do know, so you just need to get your texts into a list:

    Code:
    >>> list_of_lines = text.split('\n')
    >>> print(list_of_lines)
    ['first line', 'second line', 'third line', 'fourth line']
    >>> list_of_lines.sort()
    >>> print(list_of_lines)
    ['first line', 'fourth line', 'second line', 'third line']
    My armada: openSUSE 13.1 (home desktop, home laptop), Crunchbang Linux 11 (mini laptop, work laptop), Android 4.2.1 (tablet)
  4. #3
  5. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2005
    Posts
    588
    Rep Power
    64
    text is a string, you need to change it to a list with something like this ...
    mylist = text.split()
    then sort the list

    or ...

    Code:
    # Open a file
    fileName = input("Enter the file Name:")
    inputFile = open("C:\TextIntofile.txt", 'r')
    lineList = inputFile.readlines()
    lineList.sort()
    print('The input in alphabetical order below :')
    for line in  lineList:
        print(line)
    Last edited by Dietrich; August 14th, 2013 at 12:02 PM.
    Real Programmers always confuse Christmas and Halloween because Oct31 == Dec25
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    3
    Rep Power
    0
    Thanks guys it is working now :-)
  8. #5
  9. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,711
    Rep Power
    480
    So then, will you recant

    #fileName.write( "Python is not a cool language")


    I don't care. Learn j. www.jsoftware.com
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo