You will have write it like this: dictionary[key] = value
instead of using append
readlines()
seems to be useful in your case to, since you only
def data(name):
input_file = open(name, 'r')
di = {}
for line in input_file.readlines():
fields = line.split()
read_name = fields[0]
read_seq = fields[1]
di[read_name] = read_seq
input_file.close()
return di
print(data('sequences.txt'))
Another thing is that the code after your return statement won't run. An easy way to fix this is to write it before the statement, or use a context manager, which automatically closes the file for you. This code does the same, but is shorter, and I would way easier to read, which is always a good thing.
def data(name):
di = {}
with open(name, 'r') as file:
for line in file.readlines():
read_name, read_seq = line.split()
di[read_name] = read_seq
return di
print(data('sequences.txt'))
No comments:
Post a Comment