Trying to learn
Trying to learn

Reputation: 43

Locating matches with .csv files in python issue

I have two .csv files in which I am attempting to find exact matches between the two.

First sheet is testnasdaq.csv and it contains:

Symbol
GOGO
WFTX
SYQD
B

Second sheet is mytickers.csv and it contains:

Symbol
GOGO
WFT
QD
S

I am using this code currently, but it is not giving the correct results and I can not seem to figure out why that is.

import csv
import requests

nasdaqDatabase = r'C:\Users\Sterling\Desktop\StockProject\testnasdaq.csv'

with open(nasdaqDatabase, "r") as f:
    nasdaq_reader = csv.DictReader(f)
    nasdaq_symbols = set([row['Symbol'] for row in nasdaq_reader])
    with open(r'C:\Users\Sterling\Desktop\StockProject\mytickers.csv', 'r' ) as theFile:
        reader = csv.DictReader(theFile)
        for row in reader:
            if row['Symbol'] in nasdaq_symbols:
                print(row['Symbol'], 'FOUND')
                newAddress = 'blank.com/' + row['Symbol'] + '.htm'
                print(newAddress)

        else:
                print(row['Symbol'], 'NOT FOUND')
                newAddress = 'blank.com/' + row['Symbol'] + '.htm'
                print(newAddress)

The output that I am getting is:

GOGO FOUND
blank.com/GOGO.htm
S NOT FOUND
blank.com/S.htm

Process finished with exit code 0

Any help is appreciated.

Upvotes: 1

Views: 47

Answers (1)

adrtam
adrtam

Reputation: 7211

You opened the nasdaqDatabase as file but not read into a usable data structure. Try this:

import csv
import requests

nasdaqDatabase = r'C:\Users\Sterling\Desktop\StockProject\testnasdaq.csv'

with open(nasdaqDatabase, "r") as f:
    nasdaq_reader = csv.DictReader(f)
    nasdaq_symbols = set([row['Symbol'] for row in nasdaq_reader])
    with open(r'C:\Users\Sterling\Desktop\StockProject\mytickers.csv', 'r' ) as theFile:
        reader = csv.DictReader(theFile)
        for row in reader:
            if row['Symbol'] in nasdaq_symbols:
                print(row['Symbol'], 'FOUND')
                newAddress = 'http://eoddata.com/stockquote/NASDAQ/' + row['Symbol'] + '.htm'
                print(newAddress)
            else:
                print(row['Symbol'], 'NOT FOUND')
                newAddress = 'http://eoddata.com/stockquote/NYSE/' + row['Symbol'] + '.htm'
                print(newAddress)

Upvotes: 1

Related Questions