(Solved Homework): Python 3 presidents list Using python 3 I need help writing a program that will read presidents.txt

Python 3 presidents list

Using python 3 I need help writing a program that will read presidents.txt

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

The basic task for this lab is to read through the data file, and create an output file which will contain sentences with the information read from the data file. For example, the first line of the file is:

LincolntAbrahamt3/4/1861t4/15/1865n

The four pieces of information are:

Lincoln

Abraham

3/4/1861

4/15/1865

The output sentence for this data is:

Abraham Lincoln was president from 3/4/1861 to 4/15/1865.

Input file:

keep in mind this may display inappropriatelyas it may not format properly on this site and in actuality the code in the .txt is similar to the below example:

Buchanan(tab)James(tab)3/4/1857(tab)3/4/1861(n)

input file:

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

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 your 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.

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

Expert Answer

 Hi Attached is the Python programme
1.  Open file
2. Read eachline
3.split it in words
4.Process each arrray as the above sentence.
4. Write to the file back
## please not I still need to sort by Year###

1.put "data.txt" in the path where you  run the python filr

2.data.txt will have the above lines which needs to be parsed.

--------------------------Python Program--------------
def makeSentence(wordline):
    tot = len(wordline)
    fname = ""
    if wordline:
        for nameword in wordline[1:(tot-2)]:
           fname += nameword + " "
        president_name = fname + " " + wordline[0]
        year_servered = wordline[-2] + "  to  " + wordline[-1]
        Sentence = "{president} was president from {years} n".format(president=president_name,years=year_servered)

        fileptr.write(Sentence)
        return Sentence



def process(singleline):
    words = singleline.split()
    #print(lastname)
    newsent = makeSentence(words)
    return newsent

## Start from here
fileptr = open('output.txt','w')
with open("data.txt") as filereader:
    newline = ""
    for eachline in filereader:
        newline = process(eachline)
        





===============================Attached output.txt=======
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 
Barack  Obama was president from 1/20/2009  to  incumbant 
Benjamin  Harrison was president from 3/4/1889  to  3/4/1893 
Bill  Clinton was president from 1/20/1993  to  1/20/2001 
Calvin  Coolidge was president from 8/2/1923  to  3/4/1929 
Chester A  Arthur was president from 9/19/1881  to  3/4/1885 
Dwight D  Eisenhower was president from 1/20/1953  to  1/20/1961 
Franklin  Pierce was president from 3/4/1853  to  3/4/1857 
Franklin D  Roosevelt was president from 3/4/1933  to  4/12/1945 
George  Washington was president from 4/30/1789  to  3/4/1797 
George H W  Bush was president from 1/20/1989  to  1/20/1993 
George W  Bush was president from 1/20/2001  to  1/20/2009 
Gerald  Ford was president from 8/9/1974  to  1/20/1977 
Grover  Cleveland was president from 3/4/1885  to  3/4/1889 
Grover  Cleveland was president from 3/4/1893  to  3/4/1897 
Harry S  Truman was president from 4/12/1945  to  1/20/1953 
Herbert  Hoover was president from 3/4/1929  to  3/4/1933 
James  Madison was president from 3/4/1809  to  3/4/1817 
James  Monroe was president from 3/4/1817  to  3/4/1825 
James  Buchanan was president from 3/4/1857  to  3/4/1861 
James A  Garfield was president from 3/4/1881  to  9/19/1881 
James K  Polk was president from 3/4/1845  to  3/4/1849 
Jimmy  Carter was president from 1/20/1977  to  1/20/1981 
John  Adams was president from 3/4/1797  to  3/4/1801 
John  Tyler was president from 4/4/1841  to  3/4/1845 
John F  Kennedy was president from 1/20/1961  to  11/22/1963 
John Quincy  Adams was president from 3/4/1825  to  3/4/1829 
Lyndon B  Johnson was president from 11/22/1963  to  1/20/1969 
Buren Martin  Van was president from 3/4/1837  to  3/4/1841 
Millard  Fillmore was president from 7/9/1850  to  3/4/1853 
Richard M  Nixon was president from 1/20/1969  to  8/9/1974 
Ronald  Reagan was president from 1/20/1981  to  1/20/1989 
Rutherford B  Hayes was president from 3/4/1877  to  3/4/1881 
Theodore  Roosevelt was president from 9/14/1901  to  3/4/1909 
Thomas  Jefferson was president from 3/4/1801  to  3/4/1809 
Ulysses S  Grant was president from 3/4/1869  to  3/4/1877 
Warren G  Harding was president from 3/4/1921  to  8/2/1923 
William  McKinley was president from 3/4/1897  to  9/14/1901 
William Henry  Harrison was president from 3/4/1841  to  4/4/1841 
William Howard  Taft was president from 3/4/1909  to  3/4/1913 
Woodrow  Wilson was president from 3/4/1913  to  3/4/1921 
Zachary  Taylor was president from 3/4/1849  to  7/9/1850
iWriteHomework
Order NOW for a 10% Discount
Pages (550 words)
Approximate price: -

Why Us?

Top Quality and Well-Researched Papers

All ourbpapers are written from scratch. In fact, Clients who ask for paraphrasing services are highly discouraged. We have writers ready to craft any paper from scratch and deliver quality ahead of time.

Professional and Experienced Academic Writers

Our writers keeps you posted on your papers progress - providing you with paper outline/draft. You are also at liberty to communicate directly with your writer.

Free Unlimited Revisions

If you think we missed something, send your order for a free revision. You have 10 days to submit the order for review after you have received the final document. You can do this yourself after logging into your personal account or by contacting our support.

Prompt Delivery and 100% Money-Back-Guarantee

All papers are always delivered on time, in many cases quite ahead of time. In case we need more time to master your paper, we may contact you regarding the deadline extension. In case you cannot provide us with more time, a 100% refund is guaranteed.

Original & Confidential

We use several writing tools checks to ensure that all documents you receive are free from plagiarism. Our editors carefully review all quotations in the text. We also promise maximum confidentiality in all of our services.

24/7 Customer Support

Our support agents are available 24 hours a day 7 days a week and committed to providing you with the best customer experience. Get in touch whenever you need any assistance.

Try it now!

Calculate the price of your order

Total price:
$0.00

How it works?

Follow these simple steps to get your paper done

Place your order

Fill in the order form and provide all details of your assignment.

Proceed with the payment

Choose the payment system that suits you most.

Receive the final file

Once your paper is ready, we will email it to you.

Our Services

No need to work on your paper at night. Sleep tight, we will cover your back. We offer all kinds of writing services.

Essays

Essay Writing Service

No matter what kind of academic paper you need and how urgent you need it, you are welcome to choose your academic level and the type of your paper at an affordable price. We take care of all your paper needs and give a 24/7 customer care support system.

Admissions

Admission Essays & Business Writing Help

An admission essay is an essay or other written statement by a candidate, often a potential student enrolling in a college, university, or graduate school. You can be rest assurred that through our service we will write the best admission essay for you.

Reviews

Editing Support

Our academic writers and editors make the necessary changes to your paper so that it is polished. We also format your document by correctly quoting the sources and creating reference lists in the formats APA, Harvard, MLA, Chicago / Turabian.

Reviews

Revision Support

If you think your paper could be improved, you can request a review. In this case, your paper will be checked by the writer or assigned to an editor. You can use this option as many times as you see fit. This is free because we want you to be completely satisfied with the service offered.

× Contact Live Agents