def getelement(filename, linestart, linestop, start, stop):
with open(filename, 'rt') as handle:
return [ln[start-1:stop]
for ln in handle.readlines()[linestart-1:linestop]]
datafiles = ("data1.txt", "data2.txt")
got = [getelement(filename, 3, 4, 9, 12)
for filename in datafiles]
print "\n".join([" ".join(row) for row in zip(*got)])
追问文本很多,此处
datafiles = ("data1.txt", "data2.txt")
写成
datafiles = ("data*.txt")
行吗
追答import glob
def getelement(filename, linestart, linestop, start, stop):
with open(filename, 'rt') as handle:
return [ln[start-1:stop]
for ln in handle.readlines()[linestart-1:linestop]]
datafiles = glob.glob("data*.txt")
got = [getelement(filename, 3, 4, 9, 12)
for filename in datafiles]
print "\n".join([" ".join(row) for row in zip(*got)])本回答被提问者采纳