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 and Compile a Python Script without using Numpy that Generate an nxn matrix using Python...
Create and Compile a Python Script without using Numpy that Generate an nxn matrix using Python Script (ie n=user input) Ask (user input ) and (randomly generated) row and column location Assign Q to (known row and column location ) and 0 to all others location Please show compile script working as well
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...
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
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
Python Create a Python script file called hw3.py. Ex. 1. Write a program that inputs numbers...
Python Create a Python script file called hw3.py. Ex. 1. Write a program that inputs numbers from the user until they enter a 0 and computes the product of all these numbers and outputs it. Hint: use the example from the slides where we compute the sum of a list of numbers, but initialize the variable holding the product to 1 instead of 0. print("Enter n") n = int(input()) min = n while n != 0: if n < min:...
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...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT