Posts

Write output file, collating groups of up to 7 input lines

Image
1 $begingroup$ I have this code that reads a file and after processing a few lines writes the output to a second file: num_reads = 7 with open('data.txt') as read_file: with open('new_data.txt', 'w') as write_file: while (True): lines = try: # expect errors if the number of lines in the file are not a multiplication of num_reads for i in range(num_reads): lines.append(next(read_file)) # when the file finishes an exception occurs here #do sutff with the lines (exactly num_reads number of lines) processed = " ".join(list(map(lambda x: x.replace("n", ''), lines))) write_file.write(processed + 'n') excep...