Iterate Through Dictionary Values, Assign to Variable

1.4k Views Asked by At

I'm trying to iterate through an imported .csv file and assign each value within a column to a variable. The idea being to use that variable to conduct a search of the Shodan API, print the search results to the screen and then move on to the value in the next row in the column, assign it to the variable, do the search, and so on.

Here's what I've cobbled together from what I've found so far....

import csv

# Initialize the API
from shodan import WebAPI
api = WebAPI("My Shodan Key")

# Open csv file

with open('C:\pythonfiles\sccm.csv', 'rb') as reader:
    sccmtable = csv.reader(reader, delimiter=';')
    #for row in sccmtable:
    #print ', '.join(row)

for row in sccmtable:
    for value in row:
        edbresults = api.exploitdb.search(value)
        print (edbresults)

It seems as if this is the correct start, as I can print the content of the newly imported csv to the screen, but I'm not sure how to take the next step. Any help is greatly appreciated.

Best regards.

2

There are 2 best solutions below

5
On BEST ANSWER

In order to search each value in each row individually, do:

for row in reader:
    for value in row:
        edbresults = api.exploitdb.search(value)
        print (edbresults)
3
On

Regarding your second question, when your with statement goes out of scope, your file gets closed. You need to read its contents while still in scope. Simply indent-in.

with open('C:\pythonfiles\sccm.csv', 'rb') as reader:
    sccmtable = csv.reader(reader, delimiter=';')
    #for row in sccmtable:
    #print ', '.join(row)

    for row in sccmtable:
        for value in row:
            edbresults = api.exploitdb.search(value)
            print (edbresults)