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
  • IN PYTHON 3: Write a Python program to read the attached text file containing words, count...

    IN PYTHON 3: Write a Python program to read the attached text file containing words, count the occurrence of each unique word and store the words in alphabetical order in a text file listing the word and the total count of that word. You cannot use any advanced features such as dictionaries. The program requires user input to determine the name of the file. (example of the text file that needs to be read = for a brief moment I...

  • 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....

  • 1. Write a Python function to count how many python function definitions in a given Python...

    1. Write a Python function to count how many python function definitions in a given Python program. 2. Write a Python function to return the number of word wrapped lines of given number of characters in a given text file. Inside your function you must do it in a single line of code except docstring and comments.

  • Python DESCRIPTION Write a program that will read an array of integers from a file and...

    Python DESCRIPTION Write a program that will read an array of integers from a file and do the following: ● Task 1: Revert the array in N/2 complexity time (i.e., number of steps) . ● Task 2: Find the maximum and minimum element of the array. INPUT OUTPUT Read the array of integers from a file named “ inputHW1.txt ”. To do this, you can use code snippet from the “ file.py ” file. This file is provided in Canvas....

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