0

for loop

Hi all, I tryed to code something in pyhton, but have a challenge with a for loop. the function "find_CUA_in03" works well on the fist iteration, it picks up the "condition" from vlookup_obj and find the correct items in the "read_object" and they are wroten correctly in the "write_obj", but when "condition" chenge on second cycle it seems the "write_with_condition" is not run. I know there are milion of ways to do the same but if you would like to help me please stay with the code <i wrote below: def write_with_condition(read_obj, write_obj, condition): # """ In a file, find the lines with "condition" and write in an other file""" for line in read_obj: if condition in line: write_obj.write(line) print(line) def find_CUA_in02(original_file, condition): # Create name of dummy / temporary file only_CUA = original_file + '_CUA.txt' # Open original file in read only mode and dummy file in write mode with open(original_file, 'r') as read_obj, open(only_CUA, 'w') as write_obj: # Line by line copy data from original file to dummy file write_with_condition(read_obj, write_obj, condition) def find_CUA_in03(original_file, lookup_file): # Create name of dummy / temporary file only_CUA = original_file + '_CUA.txt' # Open original file in read only mode and dummy file in write mode and lookup file in read mode with open(original_file, 'r') as read_obj, open(only_CUA, 'w') as write_obj, open(lookup_file, 'r') as lookup_obj: for line in lookup_obj: condition = line[9:18] print(line[9:18]) write_with_condition(read_obj, write_obj, condition) https://code.sololearn.com/c6On8D4Sdf56

11th Jun 2020, 3:32 PM
Davide logos382
Davide logos382 - avatar
2 Respuestas
+ 6
You have presented us 3 functions, no main program. Please put your complete code in playground and link it here. Thanks!
11th Jun 2020, 4:05 PM
Lothar
Lothar - avatar
0
not sure we can say I wrote a "program" actually is largely incomplete as I want to debug the function "find_CUA_in03" to have the correct output before going further (and debug other existing issues). However please find in playground each and every line I have wrote so far. note that "lookup_file" do not exist at the begining, it is the output of "find_CUA_in02". https://code.sololearn.com/c6On8D4Sdf56
11th Jun 2020, 4:31 PM
Davide logos382
Davide logos382 - avatar