I am a newbie in Python ;
I would like to extract lines from a unique input file, to place them in several output files according to some specific string on each line;
it would look like the following pseudo-code (the 'line_list' variable being the list of strings in each line read from the input file)
if line_list == 'B01' then write line into 'B01-output.txt' file
if line_list == 'B02' then write line into 'B02-output.txt' file
if line_list == 'B03' then write line into 'B03-output.txt' file
and so on until, let say, B99 ;
how could I write a Python script to simplify the psuedo-code above by using loops ?
this script, if it was possible, could look like this one :
- Code: Select all
input_open = open('input_file.txt', 'r')
Bubble_List = ['B01','B02','B03','B04', ...,'B97','B98','B99']
for prefix in Bubble_List:
prefix_output_file = prefix+'_output_file.txt'
prefix_output_open = open(prefix_output_file,'w') # how to make the prefix_output_open variable name varies with the value of prefix ?
for line in input_open:
line_list = line.split()
if line_list[:3] in Bubble_List:
prefix_output_open.write(line) # how to make the prefix_output_open variable name varies with the value of prefix ?
Hope my question is clear enough
thanks in advance