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.
edit a text file with Python.
Collapse
X
-
Tags: None
-
-
What does your external program return to you? A list, dictionary, set, string? If it is a dictionary:[code=Python]>>> dd = {'line1':[1,1,1,1], 'line2':[2,2,2,2], 'line3':[3,3,3,3]}Originally posted by ironmonkey69I'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.
>>> columnzero = 1
>>> for key in dd:
... dd[key][columnzero] = 0
...
>>> dd
{'line3': [3, 0, 3, 3], 'line2': [2, 0, 2, 2], 'line1': [1, 0, 1, 1]}
>>> [/code]Comment
-
why don't you show a sample input file, a sample output file that you want?Originally posted by ironmonkey69I'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.Comment
-
-
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 outComment
-
The following code will read the file, zero the second list elements, and write the date out to another file:[code=Python]Originally posted by ironmonkey69The 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
def nthzero(dataLis t, 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 = 'infile.txt'
f = open(fn)
s = f.next()
prefix = s
while s.strip() != '#Data':
s = f.next()
prefix += s
lineList = [line.strip().sp lit() for line in f]
f.close()
elem = 1
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]Comment
-
-
[code=Python]Originally posted by ironmonkey69how can I get this code to zero out more than one column?
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 = 'indata.txt'
f = open(fn)
s = f.next()
prefix = s
while s.strip() != '#Data':
s = f.next()
prefix += s
lineList = [line.strip().sp lit() for line in f]
f.close()
elem = [1,3,5,8,12]
repl = '0'
lineList = nthzero(lineLis t, elem, repl)
print lineList
fn1 = 'outdata.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]Comment
-
Originally posted by ironmonkey69The 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 outoutput:Code:data=open("file").readlines() for num,line in enumerate(data): if num <3 : print line.strip() ; continue newlist=line.split() newlist[1] = '0' print ' '.join(newlist)
Code:#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
Comment
-
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?Comment
-
Have you tried passing the full path in the script? On Windows it would be:Originally posted by ironmonkey69right 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?
r'X:\subdir1\su bdir2\filename. ext'
Using os.path:[code=Python]import os
filename = os.path.join('X :\\', 'subdir1', 'subdir2', 'filename.ext')[/code]Comment
-
This is the code that I have:[CODE=python]
import os
filename = os.path.join('h ome', 'engine', 'ws', 'test.ext')
def nthzero(dataLis t, 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 = 'test.txt'
f = open(fn)
s = f.next()
prefix = s
while s.strip() != '#Data':
s = f.next()
prefix += s
lineList = [line.strip().sp lit() for line in f]
f.close()
elem = 1
repl = '0'
lineList = nthzero(lineLis t, elem, repl)
fn1 = 'test2.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]Comment
-
Comment