Help with some python 2.0 code

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ironmonkey69
    New Member
    • Jul 2007
    • 43

    Help with some python 2.0 code

    I need help to get this script to accept more than one number in the 'elem = ' line so that I will be able to zero more than one line at a time. Right now I am able to get it to do one line at a time. This script is written using python 2.0.

    Here is what the text file looks like:

    #Number of Bits
    12
    #Data
    0 0 0 0 0 0 0 0 0 0 0 0
    12 5 3 4 6 4 5 4 7 5 5 10
    24 9 7 7 13 7 9 9 14 10 10 20

    and this is what it does:

    #Number of Bits
    12
    #Data
    0 0 0 0 0 0 0 0 0 0 0 0
    12 0 3 4 6 4 5 4 7 5 5 10
    24 0 7 7 13 7 9 9 14 10 10 20

    Here is what the Python 2.0 code looks like:
    Code:
    def nthzero(dataList, nth, n): 
    ''' 
    Replace the nth element of each list in the data list with 'n' 
    ''' 
    for item in dataList: 
    item[nth] = n 
    return dataList 
    
    
    fn = 'outfile.txt' 
    f = open(fn) 
    
    s = f.next() 
    prefix = s 
    while s.strip() != '#Data': 
    s = f.next() 
    prefix += s 
    
    lineList = [line.strip().split() for line in f] 
    
    f.close() 
    elem = 0 
    repl = '0' 
    lineList = nthzero(lineList, elem, repl) 
    
    fn1 = 'outfile.txt' 
    f = open(fn1, 'w') 
    outList = [] 
    for line in lineList: 
    outList.append(' '.join(line)) 
    
    f.write('%s%s' % (prefix, '\n'.join(outList))) 
    f.close()
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Originally posted by ironmonkey69
    I need help to get this script to accept more than one number in the 'elem = ' line so that I will be able to zero more than one line at a time. Right now I am able to get it to do one line at a time. This script is written using python 2.0.

    Here is what the text file looks like:

    #Number of Bits
    12
    #Data
    0 0 0 0 0 0 0 0 0 0 0 0
    12 5 3 4 6 4 5 4 7 5 5 10
    24 9 7 7 13 7 9 9 14 10 10 20

    and this is what it does:

    #Number of Bits
    12
    #Data
    0 0 0 0 0 0 0 0 0 0 0 0
    12 0 3 4 6 4 5 4 7 5 5 10
    24 0 7 7 13 7 9 9 14 10 10 20

    Here is what the Python 2.0 code looks like:
    Code:
    def nthzero(dataList, nth, n): 
    ''' 
    Replace the nth element of each list in the data list with 'n' 
    ''' 
    for item in dataList: 
    item[nth] = n 
    return dataList 
    
    
    fn = 'outfile.txt' 
    f = open(fn) 
    
    s = f.next() 
    prefix = s 
    while s.strip() != '#Data': 
    s = f.next() 
    prefix += s 
    
    lineList = [line.strip().split() for line in f] 
    
    f.close() 
    elem = 0 
    repl = '0' 
    lineList = nthzero(lineList, elem, repl) 
    
    fn1 = 'outfile.txt' 
    f = open(fn1, 'w') 
    outList = [] 
    for line in lineList: 
    outList.append(' '.join(line)) 
    
    f.write('%s%s' % (prefix, '\n'.join(outList))) 
    f.close()
    This is untested. Pass a list of indices to the function nthzero(). The function has an additional loop. Watch out for indentation. The code you posted had none, so it would not work.[code=Python]def nthzero(dataLis t, nthList, n):
    '''
    Replace the nth element of each list in dataList with 'n'
    '''
    for nth in nthList:
    for item in dataList:
    try:
    item[nth] = n
    print dataList
    except IndexError, n:
    print n
    return dataList

    fn = 'outfile.txt'
    f = open(fn)

    s = f.readline()
    prefix = s
    while s.strip() != '#Data':
    s = f.readline()
    prefix += s

    lineList = [line.strip().sp lit() for line in f.readlines()]

    f.close()
    elem = [1,3,5,8,12]
    repl = '0'
    lineList = nthzero(lineLis t, elem, repl)

    fn1 = 'outfile.txt'
    f = open(fn1, 'w')

    outList = []

    for line in lineList:
    outList.append( ' '.join(line))

    f.write('%s%s' % (prefix, '\n'.join(outLi st)))
    f.close()[/code]I threw in an index of 12 to test the try-except statement.

    Comment

    • ironmonkey69
      New Member
      • Jul 2007
      • 43

      #3
      Can you add some comments to the code to help me get a better a better understanding of which sections do what?

      Comment

      Working...