Sebastian Carazo
Sebastian Carazo

Reputation: 29

Iterate over CSV file and compare strings?

So I'm working on CS50 Problem Set 6 (pset6) problem, DNA. I kinda get the logic on how to approach the problem, but I can't figure out the code. As this is a problem I should solve on my own, I'm not asking for a exact code solution, just some hints on libraries, etc.

My approach is: iterate over the first row of the database, in this case argv[1], and search for the STR's (Short Tandem Repeats) one at a time in the argv[2] or sequence file to then compare. Then I would compare the number of times in a row a STR's shows in the DNA sequence with each individual.

Link to pset6

from sys import argv, exit
import csv

if len(argv) != 3:
    print("Incorrect amount of command line arguments")
    exit(1)

with open(argv[1],'r') as database:
    pass

with open(argv[2],'r') as sequence:
    pass

Upvotes: 1

Views: 145

Answers (1)

Pascal Getreuer
Pascal Getreuer

Reputation: 3256

Check out "csv.reader":
https://docs.python.org/3/library/csv.html#csv.reader

A short usage example:

import csv
with open('eggs.csv', newline='') as csvfile:
...     spamreader = csv.reader(csvfile, delimiter=' ', quotechar='|')
...     for row in spamreader:
...         print(', '.join(row))
Spam, Spam, Spam, Spam, Spam, Baked Beans
Spam, Lovely Spam, Wonderful Spam

Upvotes: 1

Related Questions