0

I'm working on pset6 - DNA. My code is a work in progress, but the part I'm having trouble with is at the bottom. Right now I'm running without cmd line arguments, so I'm opening 1.txt and small.csv. Small.csv is:

name,AGATC,AATG,TATC

Alice,2,8,3

Bob,4,1,5

Charlie,3,2,5

from sys import argv
import os
import csv
for i in range(len(argv)):
    print(argv[i])

script_directory = os.path.dirname('__file__')

databases_directory = os.path.join(script_directory,"databases")

sequences_directory = os.path.join(script_directory,"sequences")

if (len(argv)>2):
    dna_data_file = os.path.join(sequences_directory,argv[2])
    dna_databases_file = os.path.join(databases_directory,argv[1])
else:
    dna_data_file = os.path.join(sequences_directory,"1.txt")
    dna_databases_file = os.path.join(databases_directory,"small.csv")

with open(dna_databases_file,'r') as csv_file, open(dna_data_file,'r') as dna_data:
    csv_file_dict = csv.DictReader(csv_file,delimiter = ',')
    csv_file_reader = csv.reader(csv_file,delimiter = ',')
    dna_sequence  = dna_data.read().rstrip()
    for row in csv_file_reader:
        if csv_file_reader.line_num == 1:
            sub_sequences_list = row[1:]
            print(sub_sequences_list)
            break
    for row in csv_file_dict:
        print(row)
    #checker(sub_sequences_list,dna_sequence)

When I run this code, the output is

['AGATC', 'AATG', 'TATC']
OrderedDict([('Alice', 'Bob'), ('2', '4'), ('8', '1'), ('3', '5')])
OrderedDict([('Alice', 'Charlie'), ('2', '3'), ('8', '2'), ('3', '5')])

sub_sequences_list prints as expected, but the rows in csv_file_dict do not. If I comment out the lines in the for row in csv_file_reader block, then the output is as expected:

OrderedDict([('name', 'Alice'), ('AGATC', '2'), ('AATG', '8'), ('TATC', '3')])
OrderedDict([('name', 'Bob'), ('AGATC', '4'), ('AATG', '1'), ('TATC', '5')])
OrderedDict([('name', 'Charlie'), ('AGATC', '3'), ('AATG', '2'), ('TATC', '5')])

Is this because I have two objects from the csv class that reference the same file? I would like to be able to have an object using the DictReader method and the Readermethod. Is this not possible?

You must log in to answer this question.

Browse other questions tagged .