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 is my code in python I am trying to open a file and then print...
this is my code in python I am trying to open a file and then print the contents on my console but when i run it nothing prints in the console def file_to_dictionary(rosterFile): myDictionary={}    with open(rosterFile,'r') as f: for line in f: myDictionary.append(line.strip()) print(myDictionary)             return myDictionary    file_to_dictionary((f"../data/Roster.txt"))      
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?
//Write in C++ //use this evote code and add File I/O operations (as specified in the...
//Write in C++ //use this evote code and add File I/O operations (as specified in the comments) to it. #include<iostream> using namespace std; int main() {int choice; int bezos = 0 , gates = 0 , bugs = 0 ; int vc = 0 ; do {    cout<<"\n\n\nEVOTE\n-----"    <<"\n1.Jeff Bezos    <<"\n2.Bill Gates    <<"\n3.Bugs Bunny" // 4. Print current tally [hidden admin option] // 5. Print audit trail [hidden admin option] // 6. mess with the vote...
This laboratory is designed to get you started with the project and random access file I/O....
This laboratory is designed to get you started with the project and random access file I/O. Open your source file and write or copy the code for the class interface for the Sdisk. Class Sdisk { public : Sdisk(string diskname, int numberofblocks, int blocksize); int getblock(int blocknumber, string& buffer); int putblock(int blocknumber, string buffer); int getnumberofblocks(); // accessor function int getblocksize(); // accessor function private : string diskname; // file name of software-disk int numberofblocks; // number of blocks on...
I/O Lab Java Purpose To practice the input and output concepts discussed in this module. Specifically,...
I/O Lab Java Purpose To practice the input and output concepts discussed in this module. Specifically, reading from and writing to local files, and formatting output. Instructions Read in file input.csv and generate a cleanly formatted table in output.txt. See the samples below. input.csv name,ID,salary,years experience foo,1,13890,12 bar,2,2342,3 baz,3,99999,24 output.txt Name | ID | Salary | Years experience -----+----+--------+----------------- Foo | 1 | 13890 | 12 Bar | 2 | 2342 | 3 Baz | 3 | 99999 | 24
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT