On Aug 22, 7:56 am, Chris <[EMAIL PROTECTED]> wrote: > On Aug 22, 1:14 pm, "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> wrote: > > > > > > > I am trying to take some data in file that looks like this > > > command colnum_1 columnum_2 > > > and look for the command and then cange the value in the collum(word) > > number indicated. I am under > > the impression I need enumerate but I am not sure what to do with it > > any help would be nice. > > > import sys > > > parse1filerows = [] > > csoundrows = [] > > > filename = sys.argv[0] > > number = sys.argv[1] > > outfile = open('test.sco','w') > > > infile = open(filename, 'r') > > for line in infile: > > csoundrows.append(line.split()) > > parsefile = open('parsefile1.txt', 'r') > > for line in parsefile: > > parsefile1rows.append(line.split()) > > for row in csoundrows: > > for prow in parsefile1rows: > > test = 0 > > if parsefile1[prow][0] in csoundrow[row]: > > for pcol in parsefile1[prow]: > > if test == 1: > > csoundrows[row][int(pcol)] = str(int(csoundrows[row] > > [int(pcol)] + number) > > for row in csoundrows: > > for word in rows: > > outfile.write(row) > > Rather confusing code there and non-functional. > > You never close your file handles, when finished with a file use the > .close() method > sys.argv[0] <-- the first element is the name of your .py file and > not > the first argument you supply. > When iterating over a list like csoundrows you don't need to do > for row in csoundrows: > if ... in csoundrow[row]: # This will try to use 'row' as an > index > > but rather > > if ... in row: > > Now, this is how I intepretted your question. > > from sys import argv, exit > > if len(argv) != 3: > """Ensure the correct number of arguments are supplied""" > exit('Incorrect number of arguments.') > > try: > """Checks if the Input file exists and exits if open fails.""" > inFile = open(argv[1], 'rb') > except IOError: > exit('Input file does not exist.') > > if not argv[2].isdigit(): > """Argument #2 needs to be a number""" > exit('Column number is not numerical.')
There is a number to be added to the text in that column... That is a slight edit though it still needs to be a number > > idx = int(argv[2]) > outFile = open('test.sco', 'wb') > > """Assuming your data in the parse file was a set of key, value pairs > to be used for replacement in the input file. Just splitting on > the > basic space and assigning the first element as the key and the rest > of > the string as the value to be used for replacement. > """ > replaceData = {} > for line in open('replacementInstructions.txt', 'rb'): > key = line.strip().split(' ')[0] > value = line.strip().split(' ')[1:] > replaceData[key] = value > > """Iterate over your input file, split the line into it's component > parts > and then lookup if the first element 'command' is contained in the > replacement data and if so change the data. > If you want all input to be saved into your output file, just > dedent > the 'outFile.write' line by one level and then all data will be > saved. > """ > for line in inFile: > record = line.strip().split(' ') > if record[0] in parseRows: > record[idx] = parseRows[record[0]] > outFile.write('%s\n' % ' '.join(record) ) > > inFile.close() > outFile.close()- Hide quoted text - > > - Show quoted text - I need to first find if the csound command is in the line and then go to the n'th word (a number) and add another number to it. I may need to do that for up to 5 values in the line (maybe more for cound commands that haven't been thought up yet). I then convert the data back to text. These all point to ftable's when I renumber them i.e. f1 to f3 I have to renumber them in the program file as well. .orc and .sco. If I load in a 5 from the file it means I have to load the data from the 5th column and add a number to it and save it back as text. I have found that the .close is automatically called and sometimes you can try to close a closed file causing an error.. So I let python do it.. instead of coding this in awk I try to emulate grid code that I have.. I will study this though and it does have code that is useful and helpful. this is the easy command list I will try to do lists with var number of commands by counting the number of words in a line ,'s exc.. -- http://mail.python.org/mailman/listinfo/python-list