0

I will get finance info from the alpha vantage API and I will write the answer directly in my database.

It works great but the first row of the CSV answer is the header.

How can I skip the first row?

Thank's a lot.

    #db-connection#
import mysql.connector
mydb = mysql.connector.connect(host="localhost",port="+++",user="+++",passwd="+++",database="++++")
mycursor = mydb.cursor()


#data#
from alpha_vantage.timeseries import TimeSeries
import csv
ts = TimeSeries(key='+++++', output_format='csv')
data, meta_data = ts.get_intraday(symbol='MSFT',interval='1min', outputsize='compact')
print(data)

#write data in db#

for row in data:
        mycursor.execute('INSERT INTO import (date ,open, high, low, close, volume) Values (%s,%s,%s,%s,%s,%s)',row)


mydb.commit()

print("1 record inserted, ID:", mycursor.lastrowid)

mycursor.close()
mydb.close()
print("Connection Closed")

The first row in the database is the header from the API / CSV answer.

SAVe
  • 814
  • 6
  • 22
chries8
  • 7
  • 2

2 Answers2

0

Just don't do anything with the first row?

for i, row in enumerate(data):
    if i == 0:
         continue     
    mycursor.execute('INSERT INTO import (date ,open, high, low, close, volume) Values (%s,%s,%s,%s,%s,%s)',row)
blueteeth
  • 3,330
  • 1
  • 13
  • 23
0

another idiom for expressing this is to just call next on the iterator, i.e. something like:

data_it = iter(data)
header = next(data_it)
for row in data_it:
     mycursor.execute(SQL_STATEMENT, row)
Sam Mason
  • 15,216
  • 1
  • 41
  • 60