Question

In: Computer Science

22.34 Lab 12 B FALL 2019 Python File I/O demo Overview This is a demonstration of...

22.34 Lab 12 B FALL 2019 Python File I/O demo

Overview

This is a demonstration of File IO

Description

Read in a list of states from a file, and output those states in alphabetical order.

Provided input files

A single input file named states.txt is provided that lists the 50 states of the United States - one state per row. Some states have lowercase letters, where some begin with uppercase letters. As a reminder, lowercase letters come after uppercase letters alphabetically. The states are in a random order. The file has the following format:

Michigan
Alaska
New Mexico
Alabama
Nevada
...

Objectives

Write the following in main.py:

  1. Open the states.txt file.
  2. Create a list called ‘states’ where each element in the list is one of the states read from the file.
  3. Print to the user (not to a file) an alphabetized list of all the states, with one state per line. For example,
Alabama
Alaska
Arizona
...

Solutions

Expert Solution

The file is created as states.txt name.

states.txt -

Michigan

Alaska

New Mexico

Alabama

Nevada

Montana

Nebraska

New Hampshire

New Jersey

minnesota

arkansas

california

colorado

connecticut

delaware

florida

georgia

hawaii

idaho

illinois

indiana

iowa

kansas

kentucky

louisiana

maine

maryland

massachusetts

michigan

mississippi

montana

New York

North Carolina

North Dakota

Ohio

Oklahoma

Oregon

Pennsylvania

Rhode Island

South Carolina

South Dakota

Tennessee

Texas

Utah

Vermont

Virginia

Washington

West Virginia

Wisconsin

Wyoming

missouri

nevada

new Hampshire

new Jersey

arizona

new York

north Carolina

north Dakota

ohio

nebraska

west Virginia

oregon

pennsylvania

rhode Island

south Carolina

oklahoma

south Dakota

tennessee

texas

utah

vermont

virginia

washington

new Mexico

wisconsin

wyoming

alabama

alaska

Arizona

Arkansas

California

Colorado

Connecticut

Delaware

Florida

Georgia

Hawaii

Idaho

Illinois

Indiana

Iowa

Kansas

Kentucky

Louisiana

Maine

Maryland

Massachusetts

Minnesota

Mississippi

Missouri

Then, in the program, a list is created with the name states.

All the content from the text file is printed to the user.

Program –

main.py –

states = []
# an empty list to store the name of the states

# opening a tetx file
with open("states.txt") as f:
    for ff in f:
        # takiing the elements from the text file to the list
        states.append(ff)
    
    # sorting the list    
    states = sorted(states)
    
    # displaying the name of the states
    for i in states:
        print(i)

Sample output –

Explanation –

  • An empty list states is created.
  • A states.txt file is opened.
  • In the loop, the append() method is used to add the content from the file to the list.
  • A sorted() method is used to sort the list.
  • Finally, the list is printed to the user with the loop.

Related Solutions

5) File I/O A) Compare and contrast InputStream/OutputStream based file I/O to Scanner/Printwriter based file I/O:...
5) File I/O A) Compare and contrast InputStream/OutputStream based file I/O to Scanner/Printwriter based file I/O: B) Discuss Serialization, what is it? What are the processes and features? What is the function of the keyword Transient?
Important notes: Use Console I/O (Scanner and System.out) for all user I/O in this lab. Do...
Important notes: Use Console I/O (Scanner and System.out) for all user I/O in this lab. Do not hard code data file paths or filenames; always ask the user for them. Complete the Monster Attack project by doing the following: In place of the driver used in homework 2, add a menu with a loop and switch (this is already done, ignore this). The user must be able to add attacks, get a report on existing attacks, save a file, retrieve...
Retrieve program Grades.cpp and the data file graderoll.dat from the Lab 12 folder. The code is...
Retrieve program Grades.cpp and the data file graderoll.dat from the Lab 12 folder. The code is as follows: ______________________________________________________________________________ #include // FILL IN DIRECTIVE FOR FILES #include <iostream> #include <iomanip> using namespace std; // This program reads records from a file. The file contains the // following: student's name, two test grades and final exam grade. // It then prints this information to the screen. const int NAMESIZE = 15; const int MAXRECORDS = 50; struct Grades // declares a...
This assignment will give you practice in Handling Exceptions and using File I/O. Language for this...
This assignment will give you practice in Handling Exceptions and using File I/O. Language for this program is JAVA Part One A hotel salesperson enters sales in a text file. Each line contains the following, separated by semicolons: The name of the client, the service sold (such as Dinner, Conference, Lodging, and so on), the amount of the sale, and the date of that event. Prompt the user for data to write the file. Part Two Write a program that...
What is the purpose of the XDC file? Simulate the behavioral code Map the I/O from...
What is the purpose of the XDC file? Simulate the behavioral code Map the I/O from VHDL to Basys3 Board Edit the Basys3 Board device type Run the synthesis What is the purpose of declaring a “signal”? Which file contains the list of input combinations used to simulate a circuit in Xilinx Vivado?   XDC Design Testbench What is the purpose of using ieee.std_logic_unsigned.alL or ieee.std_logic_signed.all in lab4 and lab6 testbench2?
I am trying to make a program in Python that opens a file and tells you...
I am trying to make a program in Python that opens a file and tells you how many lines, vowels, consonants, and digits there are in the file. I got the print out of lines, digits, and consonants, but the if statement I made with the vowels list isn't recognizing them. How can I make the vowels go through the if statement with the list? Am I using the wrong operator? Here is my program #Assignment Ch7-1 #This program takes...
Python 3: I have a file with 53,000 entries or so of movies, the year they...
Python 3: I have a file with 53,000 entries or so of movies, the year they were made, the rating, length, and what genre they are. I need to get user input for the year, or the title, or the rating/genre/length. The genre is a 6 digit collection of 0's and 1's. The placement of 1's determines what genre they are (or a combination of genres). The genre,rating, and length are all one test and return entries that match all...
This is all in Python. Also, the names come from a .txt file that i have...
This is all in Python. Also, the names come from a .txt file that i have created with 3 names on it(Jim,Pam,Dwight). Main Function. Write a program where the user enters a name. Using the read function, check to see if the name is in the text document. If it is, respond back to the user the name is found within the list. Read Function. The read function should return the contents of the text document as a list. EXAMPLE...
Can you please solve these questions/ statements using python? I started with "importing" the file. I...
Can you please solve these questions/ statements using python? I started with "importing" the file. I only need question one to be answered not two-four. Can use whatever data frame of choice, I just need a sample code for each line. Thank you #1. #Fit a linear regression model on data: USA_housing.csv to predict the Price of the house. import pandas as pd housing_df = pd.read_csv("USA_Housing.csv") #Income: Avg. area income #Age: Avg age of the houses #Bedrooms: Avg No of...
How would I create a nested dictionary given a csv file in Python? Say I want...
How would I create a nested dictionary given a csv file in Python? Say I want to make a dictionary that read {'country':{'China':'Fit', 'China':'Overweight', 'USA': 'Overweight', 'USA': 'Fit', 'England':'Fit'...}, 'category':{'Asian':'Fit', 'Caucasian': 'Overweight', 'Caucasian':'Overweight', 'Asian': 'Fit', 'Middle Eastern': 'Fit'...}} given a file that had country category Weight China Asian Fit China Caucasian Overweight USA Caucasian Overweight USA Asian Fit England Middle Eastern Fit... ... And so on in the file.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT