简体   繁体   中英

Getting Python to output data to Excel Sheet

I am trying to output prices given by an API for each stock symbol, into an excel sheet. The code works to output the data into the command line, but once I tried putting it into a sheet, the code gives back this error, File "C:\Users\sss\Documents\Python Programs\Bot\Td\td3.py", line 42, in get_ohlc for symbol, lastPrice in kwargs.get('symbol'): ValueError: too many values to unpack (expected 2)

I am not sure where to go from this point. The relevant code is below, and any help would be greatly appreciated !

data_sheet1 = pd.read_excel('C:\\Users\\sss\\Downloads\\companylist.xlsx', index_col=0)
data_impor = data_sheet1.head(2600)

workbook = xlsxwriter.Workbook('c:\\Users\\sss\\Downloads\\output.xlsx')
worksheet = workbook.add_worksheet()
worksheet.write('A1', 'Stock')
worksheet.write('B1', 'Price')

row = 1
col = 0

def get_ohlc(**kwargs):
    data = get_quotes(symbol=kwargs.get('symbol'))
    for symbol in kwargs.get('symbol'):
        print(symbol)
        print(data[symbol]['lastPrice'])
    for symbol, lastPrice in kwargs.get('symbol'): 
        worksheet.write(row, col, symbol) 
        worksheet.write(row, col + 1, lastPrice) 
        row += 1
    workbook.close()


for row in data_impor.index:
    get_ohlc(symbol=[row])

You can use zip to iterate through multiple columns in parallel. I'm not sure of everything that your code does, but here is an example:

Try

for symbol, lastPrice in zip(kwargs.get('symbol'), kwargs.get('lastPrice')):

Or

for symbol, lastPrice in zip(data['symbol'],data['lastPrice']):

The existing code for symbol, lastPrice in kwargs.get('symbol'): is the source of this error:

ValueError: too many values to unpack (expected 2)

You have only provided one column but two iterators, so it can't iterate unless you zip another column in or use a different method. Zip is my favorite and easiest for this problem though in my opinion.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM