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

    Join Date
    Apr 2013
    Posts
    10
    Rep Power
    0

    Remove words from a file


    Hello All,

    I am new programmer in Python environment and I would like to ask your help. I have a file with the following format.

    Te0/2/0/7 up up ASE 8222 10000000
    Te0/2/0/8 down down ASE 8222 10000000
    Te0/2/0/9 up up ASE 8222 10000000
    Te0/2/0/10 up up ASE 8222 10000000

    I would like to keep only the first and the 5th columns from the file.

    Could you please advice.

    Thank you in advance!
  2. #2
  3. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2005
    Posts
    610
    Rep Power
    65
    Something like this ...
    Code:
    # read data in as a string
    data_str = '''\
    Te0/2/0/7 up up ASE 8222 10000000
    Te0/2/0/8 down down ASE 8222 10000000
    Te0/2/0/9 up up ASE 8222 10000000
    Te0/2/0/10 up up ASE 8222 10000000'''
    
    new_data_str = ""
    for line in data_str.split('\n'):
        temp = line.split()
        new_data_str += temp[0] + ' ' + temp[4] + '\n'
    
    print(new_data_str)
    
    ''' my result ...
    Te0/2/0/7 8222
    Te0/2/0/8 8222
    Te0/2/0/9 8222
    Te0/2/0/10 8222
    '''
    Real Programmers always confuse Christmas and Halloween because Oct31 == Dec25
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Location
    /dev/null
    Posts
    163
    Rep Power
    19
    Code:
    i = open ('/home/user/file', 'r')
    for line in i:
        x = line.split();
        print (x[0] + ' ' + x[4])
    i.close()
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    10
    Rep Power
    0
    Originally Posted by noobie1000
    Code:
    i = open ('/home/user/file', 'r')
    for line in i:
        x = line.split();
        print (x[0] + ' ' + x[4])
    i.close()
    Thanks for your reply but I receive the following error:

    IndexError: list index out of range
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    10
    Rep Power
    0
    Originally Posted by Dietrich
    Something like this ...
    Code:
    # read data in as a string
    data_str = '''\
    Te0/2/0/7 up up ASE 8222 10000000
    Te0/2/0/8 down down ASE 8222 10000000
    Te0/2/0/9 up up ASE 8222 10000000
    Te0/2/0/10 up up ASE 8222 10000000'''
    
    new_data_str = ""
    for line in data_str.split('\n'):
        temp = line.split()
        new_data_str += temp[0] + ' ' + temp[4] + '\n'
    
    print(new_data_str)
    
    ''' my result ...
    Te0/2/0/7 8222
    Te0/2/0/8 8222
    Te0/2/0/9 8222
    Te0/2/0/10 8222
    '''
    The result is what I am excepting but could you please clarify the way of opening the file.
    I do:

    finn = open("file.txt", "r")
    inn = finn.readlines()
    for line in inn.split('\n'):
    .............................

    I think I am wrong
  10. #6
  11. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,839
    Rep Power
    480
    Secondly:

    finn = open("file.txt", "r")
    inn = finn.readlines()

    inn is already a list of lines of the file. Each item of the list, except perhaps the last, ends with newline. When you subsequently split an item of inn you get a list of (usually) two items, the last of which is empty.

    >>> 'the'.split('e')
    ['th', '']

    This may answer your index error trouble.


    Otherwise, Firstly: Your data is inconsistent. One of the rows doesn't have all these fields. Python handles bad data without trouble but you still need extra code. What do you want to do with bad data?
    [code]Code tags[/code] are essential for python code and Makefiles!
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    10
    Rep Power
    0
    Originally Posted by b49P23TIvg
    Secondly:

    finn = open("file.txt", "r")
    inn = finn.readlines()

    inn is already a list of lines of the file. Each item of the list, except perhaps the last, ends with newline. When you subsequently split an item of inn you get a list of (usually) two items, the last of which is empty.

    >>> 'the'.split('e')
    ['th', '']

    This may answer your index error trouble.


    Otherwise, Firstly: Your data is inconsistent. One of the rows doesn't have all these fields. Python handles bad data without trouble but you still need extra code. What do you want to do with bad data?
    Thanks for your reply. Yes, the first two rows of my file don't have all these fields. Maybe I need to read my lines after the 4th line.

IMN logo majestic logo threadwatch logo seochat tools logo