I keep coming across situations where I pull some information from a file or wherever, then have to massage the data to the final desired form through several steps. For example:
def insight_pull(file):
with open(file) as in_f:
lines = in_f.readlines()
dirty = [line.split(' ') for line in lines]
clean = [i[1] for i in dirty]
cleaner = [[clean[i],clean[i + 1]] for i in range(0, len(clean),2)]
cleanest = [i[0].split() + i[1].split() for i in cleaner]
with open("Output_File.txt", "w") as out_f:
out_f.writelines(' '.join(i) + '\n' for i in cleanest)
As per the example above:
# Pull raw data from file splitting on ' '.
dirty = [line.split(' ') for line in lines]
# Select every 2nd element from each nested list.
clean = [i[1] for i in dirty]
# Couple every 2nd element with it's predecessor into a new list.
cleaner = [[clean[i],clean[i + 1]] for i in range(0, len(clean),2)]
# Split each entry in cleaner into the final formatted list.
cleanest = [i[0].split() + i[1].split() for i in cleaner]
Seeing as I can't put all of the edits into one line or loop (since each edit depends on the edit before it), is there a better way to structure code like this?
Apologies if the question is a bit vague. Any input is much appreciated.