Question

In: Computer Science

In this task, you will create a Python script in which you will practice reading files...

In this task, you will create a Python script in which you will practice reading files in Python and writing them to a new output file. Construct a text file called Py4_Task3_input.txt that has the following lines:

4

Sandwiches 04 July 2020

Pasta 31 October 2014

Hot Dogs 15 November 2005

Tacos 25 December 1986

The first line of the file represents the number of lines in the data file. Write a loop that reads in each additional line (one line at a time) and stores this line first as a string, then converts it to a list, before moving on to reading the next line. Hint: the string for the second line should be Independence Day 04 July 2021 whereas the list for the second line should be [‘Independence’, ‘Day’, ‘04’, ‘July’, ‘2021’].

Create a second looping structure that outputs each line as a string, then as a list, before moving on to the next line. Name your output file Py4_Task3_output.txt. Name your main program Py4_Task3_teamnumber.py

Solutions

Expert Solution

Python Program:

""" Python program that reads data from file and outputs to another file """

# List to hold each line
lines = []

# Initially set numLines to 0
numLines = 0

# Opening file for reading
with open("d:\\Python\\Py4_Task3_input.txt") as fp:
  
   # Reading number of lines
   numLines = int(fp.readline())
  
   # Iterating over line by line
   for i in range(numLines):
       # Stripping new line
       line = fp.readline().strip()
       # Adding to list
       lines.append(line)      

# Opening output file
ofp = open("d:\\Python\\Py4_Task3_output.txt", "w")      
      
# Iterating over line by line
for i in range(numLines):
   # Writing to output file
   print(lines[i] + " \t " + str(lines[i].split()))
   ofp.write(lines[i] + " \t " + str(lines[i].split()) + "\n")
  
# Closing output file
ofp.close()

_________________________________________________________________________________________

Code Screenshot:

__________________________________________________________________________________________

Sample Run:


Related Solutions

Create a Python script in IDLE or Kali Python3 CLI to create the following Python Program:...
Create a Python script in IDLE or Kali Python3 CLI to create the following Python Program: Your program will create a username of your choice using a Kali Linux command "useradd -m mark", then set the password for that user using the Kali Linux Command "echo "mark:10101111" | chpasswd". Then you create a dictionary file using the Kali Linux command "crunch 8 8 01 > mylist.txt" Your python script should crack the password for that user and display on the...
create a Python script that prompts the user for a title, description, and filename of your...
create a Python script that prompts the user for a title, description, and filename of your Python program and add the following to the bottom of the existing homepage: Add the post title with an emphasis Add the post description beneath the post title Create a hyperlink to the Python file using the filename input Create another hyperlink to the page you will create in the Web Showcase assignment
Python - files: find a solution for each following: -Open the file hostdata.txt for reading. -Store...
Python - files: find a solution for each following: -Open the file hostdata.txt for reading. -Store four file objects corresponding to the files winter2003.txt , spring2003.txt, summer2003.txt, and fall2003.txt in the variables winter, spring, summer, and fall (respectively), and open them all for reading. -Write a statement to open the file yearsummary.txt in a way that erases any existing data in the file. -Use the file object output to write the string "3.14159" to a file called pi. -A file...
This week we will have an opportunity to create and practice a phone script. The purpose...
This week we will have an opportunity to create and practice a phone script. The purpose of this script is to attempt to get an appointment over the telephone. Please be sure to read both Week 4 and 5 lectures before participating. Pay particular attention to the sample script presented below. Writing a successful script is a foundation of selling. This Discussion will help you start with your sales success. Below is a sample phone script. Compare the script to...
Creation of Program Application (Development Task 1) This program should create two output files. You may...
Creation of Program Application (Development Task 1) This program should create two output files. You may use .txt files. Create the following files: Deposists.txt Withdrawls.txt 1. The program application should have the following inputs: Amount for Deposits (Only Accept Positive Amounts) Amount for Withdrawals (Only Accept Positive Amounts). The subsequent program will subtract the positive amount. 2. The program application should have the following outputs: Deposists.txt Withdrawals.txt Total of Deposits and Withdrawals back to the console
Using Python create a script called create_notes_drs.py. In the file, define and call a function called...
Using Python create a script called create_notes_drs.py. In the file, define and call a function called main that does the following: Creates a directory called CyberSecurity-Notes in the current working directory Within the CyberSecurity-Notes directory, creates 24 sub-directories (sub-folders), called Week 1, Week 2, Week 3, and so on until up through Week 24 Within each week directory, create 3 sub-directories, called Day 1, Day 2, and Day 3 Bonus Challenge: Add a conditional statement to abort the script if...
Write the following Python script: Imagine you live in a world without modules in Python! No...
Write the following Python script: Imagine you live in a world without modules in Python! No numpy! No scipy! Write a Python script that defines a function called mat_mult() that takes two lists of lists as parameters and, when possible, returns a list of lists representing the matrix product of the two inputs. Your function should make sure the lists are of the appropriate size first - if not, your program should print “Invalid sizes” and return None. Note: it...
When using random access files in Java, the programmer is permitted to create files which can...
When using random access files in Java, the programmer is permitted to create files which can be read from and written to random locations. Assume that a file called "integers.dat" contains the following values: 25 8 700 284 63 12 50 Fill in blanks: You are required to write the following Java code statements: (a) create a new stream called, blueray, which allows the program to read from and write to the file, "integers.dat." _____________________ (b) Write a statement which...
Write a Flask app in which a Python script prompts the user to enter a string...
Write a Flask app in which a Python script prompts the user to enter a string and then that string, as entered, gets displayed on an otherwise blank HTML file, called output.html. Show the Python/Flask code and the html code of the output.html file, one below the other as part of your answer.
PYTHON. Tasks: Create and test the openFileReadRobust() function to open file (for reading) whose name is...
PYTHON. Tasks: Create and test the openFileReadRobust() function to open file (for reading) whose name is provided from the standard input (keyboard) – the name is requested until it is correct Create and test the openFileWriteRobust () function to open file (for writing) whose name is provided from the standard input (keyboard) – the name is requested until is correct Extend the program to count number of characters, words and lines in a file as discussed in the classby including...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT