Question

Please help with this python assignment. Thank you. question 1 Write a Python program to read...

Please help with this python assignment. Thank you.

question 1

Write a Python program to read a file line by line store it into a variable.

question 2

Write a Python program to read a file line by line store it into an array.

question 3

Write a python program to find the longest words.

question 4

Write a Python program to count the number of lines in a text file.

question 5

Write a Python program to count the frequency of words in a file.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answer:

Explanation:

here are the codes for all the part from 1 to 5 separately.

File functions have been used, that are, read(), readline(), readlines() , etc.

Other basic functions like len() , range() are also used.

feel free to comment if you need any help!

Ques 1:

Code:

filename = input("Enter filename: ")

f = open(filename, 'r')

data = ''

line = f.readline()

while(line):
data = data+line
line = f.readline()
print(data)

filename input(Enter filename: ) f = open(filename, r) data line f.readline) while(line): data data+line line f.readline)

Output:

Enter filename: file.txt first line second line ...Program finished with exit code 0 Press ENTER to eyit console

Ques 2:

Code:


filename = input("Enter filename: ")

f = open(filename, 'r')

lines = f.readlines()

print(lines)

filename input(Enter filename: ) f open(filename, r) data lines f.readlines) print(lines)

Output:

Enter filename: file.txt [first line\n, second line] ...Program finished with exit code 0 Press ENTER to exit console.

Ques 3:

Code:


filename = input("Enter filename: ")

f = open(filename, 'r')

words = f.read().split()

max_w = words[0]

print("Longest words are:")
for word in words:
if(len(word)>len(max_w)):
max_w = word

for word in words:
if(len(max_w)==len(word)):
print(word)

filename = input(Enter filename: ) f = open(filename, r) data = words = f.read().split() max_w = words[@] print(Longest

Output:

Enter filename: file.txt Longest words are: second thirdd ..Program finished with exit code 0 Press ENTER to exit console.

Ques 4:


Code:


filename = input("Enter filename: ")

f = open(filename, 'r')

lines = f.readlines()

print("Number of lines:", len(lines))

filename = input(Enter filename: f = = open(filename, r) data lines = f.readlines (1) print(Number of lines:, len(lines)

Output:

Enter filename: file.txt Number of lines: 3 Program finished with exit code 0 Press ENTER to exit console. I

Ques 5:

Code:

filename = input("Enter filename: ")

f = open(filename, 'r')

words_freq = {}

lines = f.read().split()

for i in range(len(lines)):
lines[i] = lines[i].strip()
if lines[i] in words_freq:
words_freq[lines[i]] = words_freq[lines[i]]+1
else:
words_freq[lines[i]] = 1

print(words_freq)

filename input(Enter filename: ) f open(filename, r) words_freq { } lines f.read().split() for i in range(len(lines )): l

Output:

7 Enter filename: file.txt {second: 1, thirdd: 1, line: 3, first: 1} ...Program finished with exit code 0 Press ENTER

PLEASE UPVOTE IF YOU FOUND THIS HELPFUL!

PLEASE COMMENT IF YOU NEED ANY HELP!

Add a comment
Know the answer?
Add Answer to:
Please help with this python assignment. Thank you. question 1 Write a Python program to read...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Could I get some help with this assignment In this assignment, you'll write a program which...

    Could I get some help with this assignment In this assignment, you'll write a program which reads a text file, and prints a histogram of its word sizes. So, for example, the historgram should show the number of words of length 1, of length 2, etc., up to the number of words of length n, where n is some constant defined in your program. To obtain text files that are suitably long, you could try Project Gutenberg, a site containing...

  • Write a Python program to read lines of text from a file. For each word (i.e,...

    Write a Python program to read lines of text from a file. For each word (i.e, a group of characters separated by one or more whitespace characters), keep track of how many times that word appears in the file. In the end, print out the top twenty counts and the corresponding words for each count. Print each value and the corresponding words, in alphabetical order, on one line. Print this in reverse sorted order by word count. You can assume...

  • Can you write with comments please. Thank you. Write a Java Program that asks the user...

    Can you write with comments please. Thank you. Write a Java Program that asks the user to enter the responses on the Console and write the responses to a text file called responses.txt (in the same directory as the program) Write another JAVA prorgram to read responses.txt file and calculate the frequency of each rating in the file and output the number and frequency in two columns (number, frequency) on the Console.

  • 6. Write a program to read a text file and produce another text file in which...

    6. Write a program to read a text file and produce another text file in which all lines are less than some given length. Make sure and break lines in sensible places; for example, avoid breaking words or putting isolated punctuation marks at the beginning of a line. Please finish the question in C language programming, thank you!

  • Assignment 1 In this assignment you will be writing a tool to help you play the...

    Assignment 1 In this assignment you will be writing a tool to help you play the word puzzle game AlphaBear. In the game, certain letters must be used at each round or else they will turn into rocks! Therefore, we want to create a tool that you can provide with a list of letters you MUST use and a list of available letters and the program returns a list of all the words that contain required letters and only contain...

  • Write a python program and pseudocode The Program Using Windows Notepad (or similar program), create a...

    Write a python program and pseudocode The Program Using Windows Notepad (or similar program), create a file called Numbers.txt in your the same folder as your Python program. This file should contain a list on floating point numbers, one on each line. The number should be greater than zero but less than one million. Your program should read the following and display: The number of numbers in the file (i.e. count them) (counter) The maximum number in the file (maximum)...

  • Python 3.7 Coding assignment This Program should first tell users that this is a word analysis...

    Python 3.7 Coding assignment This Program should first tell users that this is a word analysis software. For any user-given text file, the program will read, analyze, and write each word with the line numbers where the word is found in an output file. A word may appear in multiple lines. A word shows more than once at a line, the line number will be only recorded one time. Ask a user to enter the name of a text file....

  • Write a C program to run on ocelot to read a text file and print it...

    Write a C program to run on ocelot to read a text file and print it to the display. It should optionally find the count of the number of words in the file, and/or find the number of occurrences of a substring, and/or take all the words in the string and sort them lexicographically (ASCII order). You must use getopt to parse the command line. There is no user input while this program is running. Usage: mywords [-cs] [-f substring]...

  • Program in C++! Thank you in advance! Write a menu based program implementing the following functions: (1) Write a funct...

    Program in C++! Thank you in advance! Write a menu based program implementing the following functions: (1) Write a function that prompts the user for the name of a file to output as a text file that will hold a two dimensional array of the long double data type. Have the user specify the number of rows and the number of columns for the two dimensional array. Have the user enter the values for each row and column element in...

  • //I NEED THE PROGRAM IN C LANGUAGE!// QUESTION: I need you to write a program which...

    //I NEED THE PROGRAM IN C LANGUAGE!// QUESTION: I need you to write a program which manipulates text from an input file using the string library. Your program will accept command line arguments for the input and output file names as well as a list of blacklisted words. There are two major features in this programming: 1. Given an input file with text and a list of words, find and replace every use of these blacklisted words with the string...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
Active Questions
ADVERTISEMENT