python - Array values as strings -



python - Array values as strings -

i have loop going replace strings in text file, want them replace these strings using variables have defined.

k_constant_vec = [.15707963267,.2221441469,.31415926535,.35124073655,.44428829381,.88857658763,1.33286488145]; t in range(1, len(k_constant_vec)): infile = open('/home/john/projects/simplecodes/textreplace/ex22.i') outfile = open('/home/john/projects/simplecodes/textreplace/newex22-[t].i', 'w') replacements = {'k_constant = k_constant_vec[t-1]':'k_constant = k_constant_vec[t]','file_base = out':'file_base = out-[t]'} line in infile: src, target in replacements.iteritems(): line = line.replace(src, target) outfile.write(line) infile.close() outfile.close()

i want utilize loop create bunch of new .i files. illustration create 7 .i files labeled newex22-1, newex22-2, ect each have different k_constant = k_constant_vec[1], k_constant = k_constant_vec[2], ect strings replaced in them.

thanks

there's few minor bugs/styling issues code. it's unclear why have infile. next more pythonic way of think trying do:

k_constant_vec = [.15707963267,.2221441469,.31415926535,.35124073655,.44428829381,.88857658763,1.33286488145]; file_prefix = '/home/john/projects/simplecodes/textreplace/newex22-[%s].i' index in range(len(k_constant_vec)): open(file_prefix % (index + 1), 'wb') f: f.write('k_constant = %s' % k_constant_vec[index])

python arrays string

Comments

Popular posts from this blog

model view controller - MVC Rails Planning -

ruby on rails - Devise Logout Error in RoR -

html - Submenu setup with jquery and effect 'fold' -