Python: U.S. Senate: The file Senate113.txt contains the members of the 113th U.
ID: 3910630 • Letter: P
Question
Python:
U.S. Senate: The file Senate113.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 affilation. 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 Senate113.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, D
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 Capito, West Virginia, R
Steve Daines, Montana, R
Gary Peters, Michigan, D
a, Write a program that uses the three files above to create the file Senate114.txt that contains records(each consisting of three fields) for the members of the 114th Senate where the members are ordered by state.
Need part a only
Explanation / Answer
def main(): ## Create file containing 114th Senate. infile = open("Senate113.txt", 'r') set1 = {line.rstrip() + " " for line in infile} infile.close() infile = open("RetiredSen.txt", 'r') set2 = {line.rstrip() + " " for line in infile} infile.close() infile = open("NewSen.txt", 'r') set3 = {line.rstrip() + " " for line in infile} infile.close() set1 = set1.difference(set2) set1 = set1.union(set3) listx = list(set1) listx.sort(key=lambda x: x.split(',')[1]) # sort by state outfile = open("Senate114.txt", 'w') outfile.writelines(listx) outfile.close() main()
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.