Complete the Python function read_third_line() so that it takes a filename and returns the third line of the file as a string.
def read_third_line(fname): try: f = open(fname) i = 1 for x in f: line = x.strip() if(i==3): return line i+=1 except Exception as e: pass # Testing read_third_line("file.txt")
Complete the Python function read_third_line() so that it takes a filename and returns the third line...
In Python: LoadFile is a function that takes in a string (a filename) and then returns a list. The list is the contents of the file, where each element is a list of data from the file. Here's an example of using this function. The input file had four lines of text. >>> lines = LoadFile("test.txt") >>> print("OUTPUT", lines) OUTPUT ["Hello there", "I am a test file", "please load me in and print me out", "Thanks"]
in python Complete the function read_r_line(fname, ln) to take a filename and a line number and return the contents of that line number from the file as a string. Hint: The code from Files03 will be helpful. Example: read_r_line('myf.txt', 5) will return 'I like peanut'
python 3
HW11.2. Read a file and print its lines The function takes a single string parameter, which is the name of a file. Complete the function to open the file for reading, read the lines of the file, and print out each line. The function takes a single string parameter, which function to open the file for reading, read the lines student.py def print_lines of file (filename): 1
Write a function named "loadStateDict(filename) that takes a filename and returns a dictionary of 2-character state codes and state names. The file has four columns and they are separated by commas. The first column is the state full name and the second column is the state code. You don't have to worry about column 3 & 4. You should eliminate any row that is without a state code. Save the two columns into a dictionary with key = state code...
Write a Python function makeDict() that takes a filename as a parameter. The file contains DNA sequences in Fasta format, for example: >human ATACA >mouse AAAAAACT The function returns a dictionary of key:value pairs, where the key is the taxon name and the valus is the total number of 'A' and 'T' occurrences. For example, for if the file contains the data from the example above the function should return: {'human':4,'mouse':7}
Complete the Python function read_third_line() to read the third line of the file, my2nd.txt. Return the third line as the output of function read_third_line(). Should start with def read_third_line(fname):
In python def lambda_1(filename): # Complete this function to read grades from `filename` and find the minimum # student test averages. File has student_name, test1_score, test2_score, # test3_score, test4_score, test5_score. This function must use a lambda # function and use the min() function to find the student with the minimum # test average. The input to the min function should be # a list of lines. Ex. ['student1,33,34,35,36,45', 'student2,33,34,35,36,75'] # input filename # output: (lambda_func, line_with_min_student) -- example (lambda_func, 'student1,33,34,35,36,45')...
python
Write the function getSpamLines(filename) that read the filename text file and look for lines of the form 'SPAM-Confidence: float number'. When you encounter a line that starts with "SPAM-Confidence:" extract the floating-point number on the line. The function returns the count of the lines where the confidence value is greater than 0.8. For example, if 'spam.txt' contains the following lines along with normal text getSpamLines('spam.txt') returns 2. SPAM-Confidence: 0.8945 Mary had a little lamb. SPAM-Confidence: 0.8275 SPAM-Confidence: 0.7507 The...
in python Complete the function search_file(fname, wd) to take a filename and a word. If the the word is found in the file contents, the function should return True, otherwise it should return False.
Python 3: Write a function called every_other_character() that takes in a string and returns a new string with every other character removed. That is, the new string will include the first, third, fifth, etc. letters of the given string. Must use a while or for loop in the solution.