我试图根据姓氏字母排列总统名单,尽管它正在绘制的文件目前在办公室中列出名字,姓氏,日期,并且不在办公室。
这就是我所拥有的,我需要对此做任何帮助。我已经搜寻了一些答案,其中大部分都超出了我的理解水平。我觉得我缺少一些小东西。我试图将它们全部分解成一个列表,然后对它们进行排序,但是我无法实现它,所以这就是我的起点。
INPUT_FILE = 'presidents.txt'
OUTPUT_FILE = 'president_NEW.txt'
OUTPUT_FILE2 = 'president_NEW2.txt'
def main():
infile = open(INPUT_FILE)
outfile = open(OUTPUT_FILE, 'w')
outfile2 = open(OUTPUT_FILE2,'w')
stuff = infile.readline()
while stuff:
stuff = stuff.rstrip()
data = stuff.split('\t')
president_First = data[1]
president_Last = data[0]
start_date = data[2]
end_date = data[3]
sentence = '%s %s was president from %s to %s' % \
(president_First,president_Last,start_date,end_date)
sentence2 = '%s %s was president from %s to %s' % \
(president_Last,president_First,start_date, end_date)
outfile2.write(sentence2+ '\n')
outfile.write(sentence + '\n')
stuff = infile.readline()
infile.close()
outfile.close()
main()