Python 3 please solve all A,B and C, Thank you!
Posted: Mon May 02, 2022 12:12 pm
Python 3
please solve all A,B and C, Thank you!
Creating our reference file In our initial example we create a small reference file by executing the following cell. The so-called cell-magic command &$file takes its first argument as a filename and writes the remaining content of the cell to file More about cell magic *%file composers.txt dufay ockeghem josquin Python + Code + Markdown Reading In A we recall different forms of reading text files. These are single-line answers that output some text. A: What single line is used to return the file content as a single string? Not that the string will contain the end-of-line (newline) character '\n' Markdown # YOUR CODE HERE Python assert dufay ockeghem josquin Python
B: What single line is used to return the file content as a Python list of strings. Markdown + Code + Markdown # YOUR CODE HERE Python assert == ['dufay\n', 'ockeghem\n', 'josquin\n'] Python C: This function will return the lines of the file without the 'end-of-line' characters. Use the strip method DE DE DO.. M def get_list_of_lines_without_eol_character(filename): lines = [] for line in open('composers.txt'): # YOUR CODE HERE #*************** return lines get_list_of_lines_without_eol_character('composers.txt') Python assert get_list_of_lines_without_eol_character('composers.txt') == ['dufay', 'ockeghem', 'josquin'] Python
please solve all A,B and C, Thank you!
Creating our reference file In our initial example we create a small reference file by executing the following cell. The so-called cell-magic command &$file takes its first argument as a filename and writes the remaining content of the cell to file More about cell magic *%file composers.txt dufay ockeghem josquin Python + Code + Markdown Reading In A we recall different forms of reading text files. These are single-line answers that output some text. A: What single line is used to return the file content as a single string? Not that the string will contain the end-of-line (newline) character '\n' Markdown # YOUR CODE HERE Python assert dufay ockeghem josquin Python
B: What single line is used to return the file content as a Python list of strings. Markdown + Code + Markdown # YOUR CODE HERE Python assert == ['dufay\n', 'ockeghem\n', 'josquin\n'] Python C: This function will return the lines of the file without the 'end-of-line' characters. Use the strip method DE DE DO.. M def get_list_of_lines_without_eol_character(filename): lines = [] for line in open('composers.txt'): # YOUR CODE HERE #*************** return lines get_list_of_lines_without_eol_character('composers.txt') Python assert get_list_of_lines_without_eol_character('composers.txt') == ['dufay', 'ockeghem', 'josquin'] Python