I am attempting to write both the symbol name, and the last price of the stock(which the code gets from an API) into a csv file. When I simply print out both the name and price into the command line, it works just fine. But when I tried writing these into two different columns in a csv, the symbols worked, but the code only printed the price of the last stock into the file (there should be the same number of prices as the number of symbols ~100). I don't know if this is an error in the csv writer part, or somewhere else. Thank you for any help you can give!
data_sheet1 = pd.read_excel('C:\\Users\\sss\\Downloads\\Book1.xlsx')
data_impor = data_sheet1['DDD'].tolist()
def get_ohlc(**kwargs):
data = get_quotes(symbol=kwargs.get('symbol'))
for symbol in kwargs.get('symbol'):
global lastPrice
lastPrice = (data[symbol]['lastPrice'])
get_ohlc(symbol=data_impor)
#write csv
with open ("mycsv.csv", "w" , newline='' ) as f:
thewriter = csv.writer(f)
thewriter.writerow(['col1', 'col2'])
thewriter.writerow([data_impor, lastPrice])
with open('mycsv.csv', 'r') as f:
reader = csv.DictReader(f)
for row in reader:
print(row)
What this gives:
{'col1': "['MMM', 'WBAI', 'WUBA', 'EGHT', 'AHC', 'AOS', 'ATEN', 'AIR', 'AAN', 'ABB', 'ABT', 'ABBV', 'ANF', 'AGD']", 'col2': '8.84'}
```