472,125 Members | 1,418 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,125 software developers and data experts.

Help with some python 2.0 code

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:
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
2 1156
bvdet
2,851 Expert Mod 2GB
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
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.

Similar topics

2 posts views Thread by Tomislav Lepusic | last post: by
45 posts views Thread by Joh | last post: by
3 posts views Thread by stuart_white_ | last post: by
1 post views Thread by Rahul | last post: by
37 posts views Thread by John Salerno | last post: by
3 posts views Thread by Matthew Warren | last post: by
31 posts views Thread by Mark Dufour | last post: by
12 posts views Thread by adamurbas | last post: by
reply views Thread by Ahmed, Shakir | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.