Knowee
Questions
Features
Study Tools

Define a function named read_length_per_line(filename) that takes a filename as a parameter and returns a list of integers. The function reads the contents of the file specified in the parameter and returns a list of values where each value is the length per line. For example, if a file contains the following:Hans loved to make up storiesHe would sit for hours in the tiny yardthen, the function returns [29, 39] where 29 is the number of characters in the first line and 39 is the number of characters in the second line.Here are some text files you can test your code with:names.txtmini_names.txtFor example:Test Resultprint(read_length_per_line('sentences.txt'))[29, 39]

Question

Define a function named read_length_per_line(filename) that takes a filename as a parameter and returns a list of integers. The function reads the contents of the file specified in the parameter and returns a list of values where each value is the length per line. For example, if a file contains the following:Hans loved to make up storiesHe would sit for hours in the tiny yardthen, the function returns [29, 39] where 29 is the number of characters in the first line and 39 is the number of characters in the second line.Here are some text files you can test your code with:names.txtmini_names.txtFor example:Test Resultprint(read_length_per_line('sentences.txt'))[29, 39]

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution

Sure, here is a Python function that does what you described:

def read_length_per_line(filename):
    with open(filename, 'r') as file:
        lines = file.readlines()
    return [len(line) for line in lines]

This function works as follows:

  1. It opens the file with the name provided in the filename parameter.
  2. It reads all lines from the file with the readlines() method, which returns a list of lines.
  3. It uses a list comprehension to create a new list where each element is the length of the corresponding line in the file. The len() function is used to get the length of a line.
  4. It returns the list of line lengths.

You can test this function with a file named 'sentences.txt' like this:

print(read_length_per_line('sentences.txt'))

This will print a list of integers, where each integer is the length of the corresponding line in the 'sentences.txt' file.

This problem has been solved

Similar Questions

Program to count characters spaces lines in file content

Print the number of lines, words, and characters in a file.Write a program to print the number of lines, words, and characters in a file.Note: Take the input file name from the user.Sample Test CasesTest Case 1:Expected Output:Enter·file·name:·InputData1.txtLine·count·=·7Word·count·=·51Char·count·=·306

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

File Handling:●Write a program that reads a text file and prints the number of words in it

Count the sentences, words and characters in a fileWrite a Python program that reads a file and prints the number of lines, words, and characters in the file.Input Format:The input will be a single string, which is the name of the file to be read.Output Format:The output will be three separate lines, each containing a count:The first line will contain the number of lines in the file, prefixed with "Sentences: ".The second line will contain the number of words in the file, prefixed with "Words: ".The third line will contain the number of characters in the file, prefixed with "Chars: ".Sample Test CasesTest Case 1:Expected Output:file·name:input1.txtSentences:·8Words:·34Chars:·228Test Case 2:Expected Output:file·name:input2.txtSentences:·7Words:·51Chars:·306

1/2

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.