我有两个.csv文件,其中我试图找到两者之间的完全匹配。
第一张是testnasdaq.csv,它包含:
Symbol
GOGO
WFTX
SYQD
B
第二张是mytickers.csv,它包含:
Symbol
GOGO
WFT
QD
S
我目前正在使用此代码,但它没有给出正确的结果,我似乎无法弄清楚为什么会这样。
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)
我得到的输出是:
GOGO FOUND
blank.com/GOGO.htm
S NOT FOUND
blank.com/S.htm
Process finished with exit code 0
感谢任何帮助。
答案 0 :(得分:1)
您将nasdaqDatabase作为文件打开但未读入可用的数据结构。试试这个:
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)