您试图同时在线路级别和“块”级别工作的问题是什么?
str.split() 默认已经处理了空格和制表符:
lines = open('infile.txt').readlines()
# split each line based on tabs and spaces
lines = [ x.split() for x in lines ]
# remove empty fields (assuming you want to)
lines = [ x for x in lines if len(x) > 0 ]
# transform your lists (lines) into coma separated strings
lines = [ ','.join(x) for x in lines ]
# separate lines with \n for writelines
lines = [ x + '\n' for x in lines ]
open('outfile.txt', 'w').writelines(lines)
当然,您可以组合其中的一些列表推导,为了清楚起见,我将它们分开。