471,357 Members | 1,210 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

running a python script with tcl

ironmonkey69
I am trying to run a script though a tcl ware command and cannot get it to run. I used to eclipse to write the script and was able to run it through eclipse.
Jul 26 '07 #1
15 3122
bartonc
6,596 Expert 4TB
I am trying to run a script though a tcl ware command and cannot get it to run. I used to eclipse to write the script and was able to run it through eclipse.
Help us help you by posting some of the relevant code. Instructions for using [ CODE ] tags are on the right hand side of the page when you reply to this. Thanks.
Jul 26 '07 #2
actually I figured out the problem. I am running python 2.5 when the code that I am writing is for a machine that uses python 2.0. Can you help me do the same with this code in python 2.0?

Expand|Select|Wrap|Line Numbers
  1. ...
  2. def nthzero(dataList, nth, n):
  3.     '''
  4.     Replace the nth element of each list in the data list with 'n'
  5.     '''
  6.     for item in dataList:
  7.         item[nth] = n
  8.     return dataList
  9.  
  10.  
  11. fn = 'outfile.db'
  12. f = open(fn)
  13.  
  14. s = f.next()
  15. prefix = s
  16. while s.strip() != '#Data':
  17.     s = f.next()
  18.     prefix += s
  19.  
  20. lineList = [line.strip().split() for line in f]
  21.  
  22. f.close()
  23. elem = 1
  24. repl = '0'
  25. lineList = nthzero(lineList, elem, repl)
  26.  
  27. fn1 = 'outfile.db'
  28. f = open(fn1, 'w')
  29. outList = []
  30. for line in lineList:
  31.     outList.append(' '.join(line))
  32.  
  33. f.write('%s%s' % (prefix, '\n'.join(outList)))
  34. f.close()
  35. ...
Jul 27 '07 #3
this code takes a text file with numbers that are separated by columns and zeroes out a colum. this 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

in the python code the 'elem=' statement is what chooses the column
Jul 27 '07 #4
bvdet
2,851 Expert Mod 2GB
actually I figured out the problem. I am running python 2.5 when the code that I am writing is for a machine that uses python 2.0. Can you help me do the same with this code in python 2.0?

Expand|Select|Wrap|Line Numbers
  1. ...
  2. def nthzero(dataList, nth, n):
  3.     '''
  4.     Replace the nth element of each list in the data list with 'n'
  5.     '''
  6.     for item in dataList:
  7.         item[nth] = n
  8.     return dataList
  9.  
  10.  
  11. fn = 'outfile.db'
  12. f = open(fn)
  13.  
  14. s = f.next()
  15. prefix = s
  16. while s.strip() != '#Data':
  17.     s = f.next()
  18.     prefix += s
  19.  
  20. lineList = [line.strip().split() for line in f]
  21.  
  22. f.close()
  23. elem = 1
  24. repl = '0'
  25. lineList = nthzero(lineList, elem, repl)
  26.  
  27. fn1 = 'outfile.db'
  28. f = open(fn1, 'w')
  29. outList = []
  30. for line in lineList:
  31.     outList.append(' '.join(line))
  32.  
  33. f.write('%s%s' % (prefix, '\n'.join(outList)))
  34. f.close()
  35. ...
Initially I thought the list comprehension or string methods may fail in Python 2.0, but I believe both were added in 2.0. Can you post the error message?
Jul 27 '07 #5
I'm calling the file 'monkey2.py. This is the error I have been getting:

File "monkey2.py", line 11, in ?
f = open(fn)
IOError: [Errno 2] No such file or directory: 'outfile.txt's = f.KEY_NEXT
AttributeError: 'file' object has no attribute 'KEY_NEXT'

This is the code I have been playing with:

Expand|Select|Wrap|Line Numbers
  1. ...
  2. def nthzero(dataList, nth, n):
  3.     '''
  4.     Replace the nth element of each list in the data list with 'n'
  5.     '''
  6.     for item in dataList:
  7.         item[nth] = n
  8.     return dataList
  9.  
  10.  
  11. fn = 'outfile.txt'
  12. f = open(fn)
  13.  
  14. s = f.next()
  15. prefix = s
  16. while s.strip() != '#Data':
  17.     s = f.next()
  18.     prefix += s
  19.  
  20. lineList = [line.strip().split() for line in f]
  21.  
  22. f.close()
  23. elem = 1
  24. repl = '0'
  25. lineList = nthzero(lineList, elem, repl)
  26.  
  27. fn1 = 'outfile.txt'
  28. f = open(fn1, 'w')
  29. outList = []
  30. for line in lineList:
  31.     outList.append(' '.join(line))
  32.  
  33. f.write('%s%s' % (prefix, '\n'.join(outList)))
  34. f.close()
  35. ...
I was playing with the file with a .db extension since I can't use the 'next()' in 2.0
Jul 27 '07 #6
actually that's the wrong error. I was getting attributrerror:next()
Jul 27 '07 #7
Here is the error

Traceback (most recent call last):
File "monkey.py", line 13, in ?
s = f.next()
AttributeError: next
Jul 27 '07 #8
bvdet
2,851 Expert Mod 2GB
actually that's the wrong error. I was getting attributrerror:next()
I just checked. The file.next() method was added in 2.3. Try replacing that section of code with:
Expand|Select|Wrap|Line Numbers
  1. s = f.readline()
  2. prefix = s
  3. while s.strip() != '#Data':
  4.     s = f.readline()
  5.     prefix += s
Jul 27 '07 #9
now I am getting an error:
File "monkey.py", line 19, in ?
lineList = [line.strip().split() for line in f]
TypeError: loop over non-sequence
Jul 27 '07 #10
bvdet
2,851 Expert Mod 2GB
now I am getting an error:
File "monkey.py", line 19, in ?
lineList = [line.strip().split() for line in f]
TypeError: loop over non-sequence
Let's get rid of the comp:
Expand|Select|Wrap|Line Numbers
  1. lineList = []
  2. for line in f:
  3.     lineList.append(line.strip().split())
Jul 27 '07 #11
Now I am getting this error:

Traceback (most recent call last):
File "monkey.py", line 20, in ?
for line in f:
TypeError: loop over non-sequence
Jul 27 '07 #12
bvdet
2,851 Expert Mod 2GB
Now I am getting this error:

Traceback (most recent call last):
File "monkey.py", line 20, in ?
for line in f:
TypeError: loop over non-sequence
??
Expand|Select|Wrap|Line Numbers
  1. lineList = [line.strip().split() for line in f.readlines()]
Do you understand what is taking place here? Since there was no next() method in Python 2.0, this was the way to iterate on a file (I am concluding this by deduction).
Jul 27 '07 #13
I'm still getting the sam error:

Traceback (most recent call last):
File "testing.py", line 20, in ?
for line in f:
TypeError: loop over non-sequence
Jul 27 '07 #14
bvdet
2,851 Expert Mod 2GB
I'm still getting the sam error:

Traceback (most recent call last):
File "testing.py", line 20, in ?
for line in f:
TypeError: loop over non-sequence
Please read my last post!

for line in f.readlines():
Jul 27 '07 #15
can you help me make this take in a command line argument and have it loop the zero'ing out of the columns until all the numbers that were inputted through the command line.
Jul 27 '07 #16

Post your reply

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

Similar topics

7 posts views Thread by Arun | last post: by
1 post views Thread by faxme | last post: by
8 posts views Thread by Paul Cochrane | last post: by
2 posts views Thread by Benjamin Rutt | last post: by
6 posts views Thread by Guillermo | last post: by
reply views Thread by XIAOLAOHU | 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.