By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,715 Members | 768 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,715 IT Pros & Developers. It's quick & easy.

Help with some python 2.0 code

ironmonkey69
P: 43
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:
Expand|Select|Wrap|Line Numbers
  1. def nthzero(dataList, nth, n): 
  2. ''' 
  3. Replace the nth element of each list in the data list with 'n' 
  4. ''' 
  5. for item in dataList: 
  6. item[nth] = n 
  7. return dataList 
  8.  
  9.  
  10. fn = 'outfile.txt' 
  11. f = open(fn) 
  12.  
  13. s = f.next() 
  14. prefix = s 
  15. while s.strip() != '#Data': 
  16. s = f.next() 
  17. prefix += s 
  18.  
  19. lineList = [line.strip().split() for line in f] 
  20.  
  21. f.close() 
  22. elem = 0 
  23. repl = '0' 
  24. lineList = nthzero(lineList, elem, repl) 
  25.  
  26. fn1 = 'outfile.txt' 
  27. f = open(fn1, 'w') 
  28. outList = [] 
  29. for line in lineList: 
  30. outList.append(' '.join(line)) 
  31.  
  32. f.write('%s%s' % (prefix, '\n'.join(outList))) 
  33. f.close()
  34.  
Aug 3 '07 #1
Share this Question
Share on Google+
2 Replies


bvdet
Expert Mod 2.5K+
P: 2,851
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:
Expand|Select|Wrap|Line Numbers
  1. def nthzero(dataList, nth, n): 
  2. ''' 
  3. Replace the nth element of each list in the data list with 'n' 
  4. ''' 
  5. for item in dataList: 
  6. item[nth] = n 
  7. return dataList 
  8.  
  9.  
  10. fn = 'outfile.txt' 
  11. f = open(fn) 
  12.  
  13. s = f.next() 
  14. prefix = s 
  15. while s.strip() != '#Data': 
  16. s = f.next() 
  17. prefix += s 
  18.  
  19. lineList = [line.strip().split() for line in f] 
  20.  
  21. f.close() 
  22. elem = 0 
  23. repl = '0' 
  24. lineList = nthzero(lineList, elem, repl) 
  25.  
  26. fn1 = 'outfile.txt' 
  27. f = open(fn1, 'w') 
  28. outList = [] 
  29. for line in lineList: 
  30. outList.append(' '.join(line)) 
  31.  
  32. f.write('%s%s' % (prefix, '\n'.join(outList))) 
  33. f.close()
  34.  
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.
Expand|Select|Wrap|Line Numbers
  1. def nthzero(dataList, nthList, n):
  2.     '''
  3.     Replace the nth element of each list in dataList with 'n'
  4.     '''
  5.     for nth in nthList:
  6.         for item in dataList:
  7.             try:
  8.                 item[nth] = n
  9.                 print dataList
  10.             except IndexError, n:
  11.                 print n
  12.     return dataList
  13.  
  14. fn = 'outfile.txt' 
  15. f = open(fn) 
  16.  
  17. s = f.readline()
  18. prefix = s
  19. while s.strip() != '#Data':
  20.     s = f.readline()
  21.     prefix += s
  22.  
  23. lineList = [line.strip().split() for line in f.readlines()] 
  24.  
  25. f.close() 
  26. elem = [1,3,5,8,12]
  27. repl = '0' 
  28. lineList = nthzero(lineList, elem, repl) 
  29.  
  30. fn1 = 'outfile.txt' 
  31. f = open(fn1, 'w')
  32.  
  33. outList = []
  34.  
  35. for line in lineList:
  36.     outList.append(' '.join(line))
  37.  
  38. f.write('%s%s' % (prefix, '\n'.join(outList)))
  39. f.close()
I threw in an index of 12 to test the try-except statement.
Aug 3 '07 #2

ironmonkey69
P: 43
Can you add some comments to the code to help me get a better a better understanding of which sections do what?
Aug 16 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.