Answered! Python sort list in Python 3 This is a second request as the first request was filled, but it does not meet the objective of what I am trying to…

Python sort list in Python 3

This is a second request as the first request was filled, but it does not meet the objective of what I am trying to produce and as well produces a blank output I appreciate the first attempt at helping me with this code, but unfortunately it is not working and does not produce the output that i need.

Don't use plagiarized sources. Get Your Custom Essay on
Answered! Python sort list in Python 3 This is a second request as the first request was filled, but it does not meet the objective of what I am trying to…
GET AN ESSAY WRITTEN FOR YOU FROM AS LOW AS $13/PAGE
Order Essay

Using the following input labeled presidents.txt:

Lincoln Abraham 3/4/1861        4/15/1865
Jackson Andrew 3/4/1829        3/4/1837
Johnson Andrew 4/15/1865       3/4/1869
Obama   Barack 1/20/2009       incumbant
Harrison        Benjamin        3/4/1889        3/4/1893
Clinton Bill    1/20/1993       1/20/2001
Coolidge        Calvin 8/2/1923        3/4/1929
Arthur Chester A       9/19/1881       3/4/1885
Eisenhower      Dwight D        1/20/1953       1/20/1961
Pierce Franklin        3/4/1853        3/4/1857
Roosevelt       Franklin D      3/4/1933        4/12/1945
Washington      George 4/30/1789       3/4/1797
Bush    George H W      1/20/1989       1/20/1993
Bush    George W        1/20/2001       1/20/2009
Ford    Gerald 8/9/1974        1/20/1977
Cleveland       Grover 3/4/1885        3/4/1889
Cleveland       Grover 3/4/1893        3/4/1897
Truman Harry S 4/12/1945       1/20/1953
Hoover Herbert 3/4/1929        3/4/1933
Madison James   3/4/1809        3/4/1817
Monroe James   3/4/1817        3/4/1825
Buchanan        James   3/4/1857        3/4/1861
Garfield        James A 3/4/1881        9/19/1881
Polk    James K 3/4/1845        3/4/1849
Carter Jimmy   1/20/1977       1/20/1981
Adams   John    3/4/1797        3/4/1801
Tyler   John    4/4/1841        3/4/1845
Kennedy John F 1/20/1961       11/22/1963
Adams   John Quincy     3/4/1825        3/4/1829
Johnson Lyndon B        11/22/1963      1/20/1969
Van Buren       Martin 3/4/1837        3/4/1841
Fillmore        Millard 7/9/1850        3/4/1853
Nixon   Richard M       1/20/1969       8/9/1974
Reagan Ronald 1/20/1981       1/20/1989
Hayes   Rutherford B    3/4/1877        3/4/1881
Roosevelt       Theodore        9/14/1901       3/4/1909
Jefferson       Thomas 3/4/1801        3/4/1809
Grant   Ulysses S       3/4/1869        3/4/1877
Harding Warren G        3/4/1921        8/2/1923
McKinley        William 3/4/1897        9/14/1901
Harrison        William Henry   3/4/1841        4/4/1841
Taft    William Howard 3/4/1909        3/4/1913
Wilson Woodrow 3/4/1913        3/4/1921
Taylor Zachary 3/4/1849        7/9/1850

Each line in this file has four pieces of information, separated by tabs.

Last name t First name t Date took office t Date left office n

LincolntAbrahamt3/4/1861t4/15/1865n

The four pieces of information are:

Lincoln

Abraham

3/4/1861

4/15/1865

I need help with a python 3 script that will read from presidents.txt and produce 3 seperate .txt files

The first output file needs to be named same_order.txt, thus far my pseudo code is:

open the input file
open the output file
for each line in the input file:
call function to write out the sentence
close the input file
close the output file

with an output of:

Abraham Lincoln was president from 3/4/1861 to 4/15/1865.
Andrew Jackson was president from 3/4/1829 to 3/4/1837.
Andrew Johnson was president from 4/15/1865 to 3/4/1869.

the second output file needs to be named last_name.txt and have an output like this:

John Adams was president from 3/4/1797 to 3/4/1801.
John Quincy Adams was president from 3/4/1825 to 3/4/1829.
Chester A Arthur was president from 9/19/1881 to 3/4/1885.

In the cases where the last names are the same, the Johnsons, the Adams’s, and the Bushes, sort by first name

The third and final output file needs to be named inauguration.txt with an output that looks like this:

George Washington was president from 4/30/1789 to 3/4/1797.
John Adams was president from 3/4/1797 to 3/4/1801.
Thomas Jefferson was president from 3/4/1801 to 3/4/1809.

for both William Henry Harrison and James A Garfield who served less than one year as president. I should include the month that the president took office as the order of data, not just the year.

I need to mainly keep the data ordered within code, rather than trying to rearrange the input data file

Expert Answer

 import sys,os

import datetime as dt
import operator
class president:
lastname=”
firstname=”
dto=dt.datetime.today().strftime(“%m/%d/%Y”)
dlo=dt.datetime.today().strftime(“%m/%d/%Y”)

pr=[]
i=0
with open(‘presidents.txt’) as f:
for line in f:
obj=president()
list=line.split(“t”)
obj.lastname=list[0];
obj.firstname=list[1];
pr.append(obj.lastname)
pr.append(obj.firstname)
date_split=list[2].split(“/”)
obj.dto=dt.datetime(int(date_split[2]),int(date_split[1]),int(date_split[0]),0,0)
pr.append(obj.dto)
date_split=list[2].split(“/”)
obj.dlo=dt.datetime(int(date_split[2]),int(date_split[1]),int(date_split[0]),0,0)
pr.append(obj.dlo)
i=i+1
map(operator.itemgetter(0), pr)
map(operator.itemgetter(1), pr)
map(operator.itemgetter(2), pr)
map(operator.itemgetter(3), pr)
file_path=’same_order.txt’
orig_stdout = sys.stdout
f = file(file_path, ‘w’)
sys.stdout = f
print sorted(L, key=operator.itemgetter(1))
sys.stdout = orig_stdout
file_path=’last_name.txt’
orig_stdout = sys.stdout
f = file(file_path, ‘w’)
sys.stdout = f
print sorted(L, key=operator.itemgetter(0))
sys.stdout = orig_stdout

Please slightly modify this to get the answer as i need more time to submit,

Still stressed from student homework?
Get quality assistance from academic writers!