(Dec-21-2019, 06:53 PM)DreamingInsanity Wrote: You can do something like this:
lines = infile.readlines() #put all the lines in an array sizeOfGroups = 2 if len(lines) % sizeOfGroups == 1: #if it's an odd number of elements lines.remove(lines[-1]) #Remove the last element - you could always add a new element to the end intead of removing. groups = [lines[n:n+sizeOfGroups] for n in range(0, len(lines), sizeOfGroups)] #use a list comprehension to split the big list into smaller sub lists with the size of 'sizeOfGroups' for group in groups: #loop through each esub list outfile.write('({0}=={1}) and\n'.format(group[0].replace('\n',''), group[1].replace('\n',''))) #format and write to the outfile outfile.close()As it says in the comments of line 4, it removes the last element if the list is an odd number. You can always change it to this, so it adds a filler element:
lines = lines + ['-----']In my opinion, this doesn't give you a very pretty output because it would be something like this:
Note: the list comprehension on line 5 is the same as this:
Output:(000123461==-----) and
sub_lists = [] for n in range(0, len(lines), sizeOfGroups): #the extra parameter in range() tells it how much it increases by each time - for this it goes: 2, 4, 6 .... sub_lists.append(lines[n:n+sizeOfGroups]) #split the list into groups by taking the element 0-2 (2 not included so technically speaking 0-1) and so on.
DreamingInsanity!
Thanks for your help and input - Merry Christmas!
// J