我经常遇到从文件或任何地方提取一些信息的情况,然后必须通过几个步骤将数据按摩到最终所需的形式。例如:
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)
按照上面的例子:
# 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]
鉴于我不能将所有编辑放入一行或循环中(因为每个编辑都取决于之前的编辑),有没有更好的方法来构建这样的代码?
如果问题有点含糊,请道歉。非常感谢任何输入。