Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Using python, write code for problem. The text files Senate113.txt, RetiredSen.t

ID: 3847990 • Letter: U

Question

Using python, write code for problem. The text files Senate113.txt, RetiredSen.txt and NewSen.txt are used in this problem and i have pasted contents of the files. Please also put screenshot of your code.

Senate113.txt

RetiredSen.txt

Senate114.txt

4. U.S. Senate The file senatel13.txt contains the members of the 113th U.S. Senate that is, the Senate prior to the November 2014 election. Each record of the file consists of three fields name, state, and party affiliation. Some records in the file are as follows Richard Shelby ,Alabama,R Bernard Sanders,Vermont,I Kristen Gillibrand,New York,D The file Retiredsen.txt contains the records from the file senatel13.txt for senators who left the Senate after the November 2014 election due to retirement, defeat, death, or resignation. Some records in the file are as follows: John Rockefeller, West Virginia, Tom Coburn,Oklahoma,R Carl Levin, Michigan,D The file Newsen.txt contains records for the senators who were newly elected in November 2014 or who were appointed to fill the seats of senators who left after the November 2014 election. Some records in the file are as follows: Shelly West Virginia ,R Capito, Steve Daines, Montana,R Gary Peters, Michigan,D

Explanation / Answer


1)
Code:

#!/usr/local/bin/python3


# script name: createSen.py
# create senate file Senate114.txt from Senate113.txt and RetiredSen.txt

def main():
sen = {}
# opening the Senate113.txt in read to store in dictionary sen
with open("Senate113.txt") as fp:
for line in fp:
line = line.strip()
name = line.split(',')[0]
sen[name] = line
fp.close()
  
# opening the file RetiredSen.txt to compare Senate file and delete the entries common in both as some of them are retired
with open("RetiredSen.txt") as fp:
for line in fp:
line = line.strip()
name = line.split(',')[0]
if name in sen and line == sen[name]:
del sen[name]
fp.close()

sen_list = []
# storing into list the dictionary of Senator file
for name in sen:
fields = sen[name].split(',')
sen_list.append(fields)
# sorting the list by state
sen_list_sorted = sorted(sen_list, key=lambda x: (x[1]))

# writing to new file Senate114.txt the sorted list
fout = open("Senate114.txt", "w")
for item in sen_list_sorted:
name, state, pa = item
print(name+","+state+","+pa, file=fout)
fout.close()


if __name__=='__main__':
main()

Execution and output:
Unix Terminal> python3 createSen.py
Unix Terminal>


2)
Code:

#!/usr/local/bin/python

# program to determine number of senators of each party
# script name: numSenator.py

def main():
# opening the file Senate114 for reading
with open("Senate114.txt") as fp:
num_sen = {}
for line in fp:
line = line.strip()
# getting name, state, pa from the file
name, state, pa = line.split(',')
# assigning key the appropriate value based on pa code
if pa == 'I':
key = 'Independents'
elif pa == 'R':
key = 'Republicans'
elif pa == 'D':
key = 'Democrats'
# counting how many Independents, Republicans and Democrats are available
if key in num_sen:
num_sen[key] += 1
else:
num_sen[key] = 1
fp.close()
# printing the final output
print "Party Affiliations:"
for pa in num_sen:
print " " + pa + ":" + str(num_sen[pa])

if __name__=='__main__':
main()


Execution and output:
Unix Terminal> python numSenator.py
Party Affiliations:
Independents:2
Republicans:42
Democrats:43
Unix Terminal>


NOTE: Due to lack of time i was not able to complete the last 2 which i will share the code by EOD today.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote