I got this assignment, which im stucked on and hoping that anyone can help me.
In the exam.py file, write a function called read_speeches that takes a speech filename as argument, and returns a dictionary, where the keys are the title of the speeches in the file (as strings), and the values are the corresponding speeches (as strings). You should remove the "#" character from the titles. Replace \n characters in the speech lines with a single space. Also, all speech lines starting with "[" should be omitted. Remember to close the files after reading from them.
In the exam_test.py file, call the read_speeches on both the clinton_speeches.txt and the trump_speeches.txt files, and save the results in two variables called clinton_speeches_dict and trump_speeches_dict, respectively. Print out the size (number of keys) in each of these two dictionaries.
So far my function looks like this (dunno if its done correct): def read_speeches(path_of_fasta):
input = open(path_of_fasta)
input_r = input.readlines()
fasta_dict = {}
for line in input_r:
line = line.strip()
if line.startswith('#'):
key = line.replace("#", "") fasta_dict[key] = ""
if line.startswith('\n'):
key = line.replace("\n", "")
fasta_dict[key] = ""
if line.startswith('['):
key = line.replace("[", "")
fasta_dict[key] = ""
else: fasta_dict[key] += line
return fasta_dict
Im having problems with writing the test, which should count the total number of keys in each dictionaries. This is what i have tried:
import exam result = exam.read_speeches('clinton_speeches.txt') print len(result['key']) And i get the feedback print len(result['key']) ^ SyntaxError: invalid syntax
Aucun commentaire:
Enregistrer un commentaire