csv - Parameter study using python -


i have file called pattern.pc content this:

<p1> bla bla  <p2> bla bla 

now replace <p1> , <p2> values in separate file (let parameter_values.pc) e.g values p1 in first line , p2 in second line:

1.0, 2.0, 3.0, 4.0 10.0, 11.0, 12.0, 13.0 

and new files (here p1 = 1.0 , p2 = 10.0):

1.0 bla bla 10.0 bla bla 

which named e.g pattern_p1_1_p2_10.pc , on. in python. forgot mention possible combinations between parameters values acceptable - in particular case have @ end 16 new files. thank

# assumes python 2.7  parameters_file = "parameter_values.pc" pattern_file    = "pattern.pc" output_name     = "pattern_p1_{p1}_p2_{p2}.pc"  def strs(line, split_on=none):     return [s.strip() s in line.split(split_on)]  def main():     open(pattern_file) inf:         template = inf.read()      open(parameters_file) inf:         p1_lst = strs(next(inf, ""), ",")         p2_lst = strs(next(inf, ""), ",")      p1, p2 in zip(p1_lst, p2_lst):         fname = output_name.format(p1=p1, p2=p2)         data = template.replace("<p1>", p1).replace("<p2>", p2)         open(fname, "w") outf:             outf.write(data)  if __name__=="__main__":     main() 

edit: ok, if want combinations,

from itertools import product 

and replace for p1, p2 in zip(p1_lst, p2_lst): for p1, p2 in product(p1_lst, p2_lst):.


Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -