471,344 Members | 1,502 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

edit a text file with Python.

ironmonkey69
I'm learning Python and I need some help. I have a text file with 12 columns filled with numbers. I need to write a script that would be able to select a column and zero out all the values in that column. I don't need to worry about grabbing the file or saving it because I'm useing an extrnal program to do all of that. I just need help with that part that zero's otu the specified column.
Jul 19 '07 #1
16 4433
also that text file has a some text before the columns in separate line.
Jul 19 '07 #2
bvdet
2,851 Expert Mod 2GB
I'm learning Python and I need some help. I have a text file with 12 columns filled with numbers. I need to write a script that would be able to select a column and zero out all the values in that column. I don't need to worry about grabbing the file or saving it because I'm useing an extrnal program to do all of that. I just need help with that part that zero's otu the specified column.
What does your external program return to you? A list, dictionary, set, string? If it is a dictionary:
Expand|Select|Wrap|Line Numbers
  1. >>> dd = {'line1':[1,1,1,1], 'line2':[2,2,2,2], 'line3':[3,3,3,3]}
  2. >>> columnzero = 1
  3. >>> for key in dd:
  4. ...     dd[key][columnzero] = 0
  5. ...     
  6. >>> dd
  7. {'line3': [3, 0, 3, 3], 'line2': [2, 0, 2, 2], 'line1': [1, 0, 1, 1]}
  8. >>> 
Jul 19 '07 #3
ghostdog74
511 Expert 256MB
I'm learning Python and I need some help. I have a text file with 12 columns filled with numbers. I need to write a script that would be able to select a column and zero out all the values in that column. I don't need to worry about grabbing the file or saving it because I'm useing an extrnal program to do all of that. I just need help with that part that zero's otu the specified column.
why don't you show a sample input file, a sample output file that you want?
Jul 19 '07 #4
How can you tell if it is a list, dictionary, set, string?
Jul 19 '07 #5
bvdet
2,851 Expert Mod 2GB
How can you tell if it is a list, dictionary, set, string?
When the object is printed to the screen or your IDE interactive window, you should be able to tell. We need more info.
Jul 19 '07 #6
The actual text file is really long (a total of 255 rows), but here is what the start of the input 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

These are the first three rows,

Here is the desired output(also first three rows:

#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 this I wanted the second column to be zero'd out
Jul 19 '07 #7
bvdet
2,851 Expert Mod 2GB
The actual text file is really long (a total of 255 rows), but here is what the start of the input 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

These are the first three rows,

Here is the desired output(also first three rows:

#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 this I wanted the second column to be zero'd out
The following code will read the file, zero the second list elements, and write the date out to another file:
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. fn = 'infile.txt'
  10. f = open(fn)
  11.  
  12. s = f.next()
  13. prefix = s
  14. while s.strip() != '#Data':
  15.     s = f.next()
  16.     prefix += s
  17.  
  18. lineList = [line.strip().split() for line in f]
  19.  
  20. f.close()
  21. elem = 1
  22. repl = '0'
  23. lineList = nthzero(lineList, elem, repl)
  24.  
  25. fn1 = 'outfile.txt'
  26. f = open(fn1, 'w')
  27. outList = []
  28. for line in lineList:
  29.     outList.append(' '.join(line))
  30.  
  31. f.write('%s%s' % (prefix, '\n'.join(outList)))
  32. f.close()
Jul 19 '07 #8
how can I get this code to zero out more than one column?
Jul 19 '07 #9
bvdet
2,851 Expert Mod 2GB
how can I get this code to zero out more than one column?
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 = 'indata.txt'
  15. f = open(fn)
  16.  
  17. s = f.next()
  18. prefix = s
  19. while s.strip() != '#Data':
  20.     s = f.next()
  21.     prefix += s
  22.  
  23. lineList = [line.strip().split() for line in f]
  24.  
  25. f.close()
  26. elem = [1,3,5,8,12]
  27. repl = '0'
  28. lineList = nthzero(lineList, elem, repl)
  29. print lineList
  30. fn1 = 'outdata.txt'
  31. f = open(fn1, 'w')
  32. outList = []
  33. for line in lineList:
  34.     outList.append(' '.join(line))
  35.  
  36. f.write('%s%s' % (prefix, '\n'.join(outList)))
  37. f.close()
Jul 19 '07 #10
ghostdog74
511 Expert 256MB
The actual text file is really long (a total of 255 rows), but here is what the start of the input 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

These are the first three rows,

Here is the desired output(also first three rows:

#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 this I wanted the second column to be zero'd out
Expand|Select|Wrap|Line Numbers
  1. data=open("file").readlines()
  2. for num,line in enumerate(data):
  3.     if num <3 : print line.strip() ; continue
  4.     newlist=line.split()
  5.     newlist[1] = '0'
  6.     print ' '.join(newlist)
  7.  
output:
Expand|Select|Wrap|Line Numbers
  1. #Number of Bits
  2. 12
  3. #Data
  4. 0 0 0 0 0 0 0 0 0 0 0 0
  5. 12 0 3 4 6 4 5 4 7 5 5 10
  6. 24 0 7 7 13 7 9 9 14 10 10 20
  7.  
Jul 20 '07 #11
right now I am using eclipse to write the python code. It uses the root directory of the project when calling a file. Is there as way that I can access a file from a specific directory. On a network through linux?
Jul 23 '07 #12
bvdet
2,851 Expert Mod 2GB
right now I am using eclipse to write the python code. It uses the root directory of the project when calling a file. Is there as way that I can access a file from a specific directory. On a network through linux?
Have you tried passing the full path in the script? On Windows it would be:
r'X:\subdir1\subdir2\filename.ext'
Using os.path:
Expand|Select|Wrap|Line Numbers
  1. import os
  2. filename = os.path.join('X:\\', 'subdir1', 'subdir2', 'filename.ext')
Jul 23 '07 #13
This is the code that I have:
Expand|Select|Wrap|Line Numbers
  1. import os
  2. filename = os.path.join('home', 'engine', 'ws', 'test.ext')
  3. def nthzero(dataList, nth, n):
  4.     '''
  5.     Replace the nth element of each list in the data list with 'n'
  6.     '''
  7.     for item in dataList:
  8.         item[nth] = n
  9.     return dataList
  10.  
  11. fn = 'test.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 = 'test2.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()
Jul 24 '07 #14
can someone explain to me step by step what is going on?
Jul 24 '07 #15
bartonc
6,596 Expert 4TB
can someone explain to me step by step what is going on?
First things first!!! Notice the "reason for editing" at the bottom of your latest post.

[ CODE ] tags are required by the Posting Guidelines of this site. Instructions for using them are on the right and side of the page while you are posting or replying. Consider this a very stern warning.
Jul 24 '07 #16
on the command line, how would I pass on the list of numbers of the columns that I want to zero out?
Jul 26 '07 #17

Post your reply

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

Similar topics

reply views Thread by Simon Burton | last post: by
8 posts views Thread by John Salerno | last post: by
3 posts views Thread by cyberco | last post: by
1 post views Thread by Xicon | last post: by
reply views Thread by Ronak mishra | 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.