Reputation:
My program for getting Crypto price data won't write to the CSV file Python3. It reads just fine and there are no errors, but it won't write to the file. I have tried the section by itself without all the other stuff, and it works, but for some reason when I put it in here it just won't write any data to the CSV.
Code: (the 'if time =' sections is to get the price every 15 mins, and to close the script at 6:50 to prepare for daily system reboot)
import requests
import json
from datetime import datetime
from time import sleep
import csv
try:
requests.get('https://api.binance.com/api/v3/ping').json()
except:
print("Connection failed")
time = str(datetime.now().time())[:5]
def get_data():
time = str(datetime.now().time())[:5]
print("\n\nTime: " + time + '\n')
with open('BTCAUD_data.csv', mode='r') as BTCAUD_data:
reader = csv.reader(BTCAUD_data, delimiter=',')
data = list(reader)[-1]
previous_buy_price = data[6]
previous_sell_price = data[8]
print(previous_sell_price)
print(previous_buy_price)
data = requests.get('https://api.binance.com/api/v3/ticker/24hr', {"symbol": "BTCAUD"}).json()
raw_price_change = data["priceChange"]
print(raw_price_change)
price_change_percent = data["priceChangePercent"]
print(price_change_percent)
weighted_avg = data["weightedAvgPrice"]
print(weighted_avg)
last_price = data["lastPrice"]
print(last_price)
last_qty = data["lastQty"]
print(last_qty)
bid_price = data["bidPrice"]
print(bid_price)
bid_qty = data["bidQty"]
print(bid_qty)
ask_price = data["askPrice"]
print(last_price)
ask_qty = data["askQty"]
print(ask_qty)
open_price = data["openPrice"]
print(open_price)
high_price = data["highPrice"]
print(high_price)
low_price = data["lowPrice"]
print(low_price)
trading_volume = data["volume"]
print(trading_volume)
quote_volume = data["quoteVolume"]
print(quote_volume)
with open('BTCAUD_data.csv', mode='w', newline='\n') as BTCAUD_data:
writer = csv.writer(BTCAUD_data)
writer.writerow([bid_price, ask_price,'\n'])
writer.writerow([previous_buy_price, previous_sell_price, raw_price_change, price_change_percent, weighted_avg, last_price, last_qty, bid_price, bid_qty, ask_price, ask_qty, open_price, high_price, low_price, trading_volume, quote_volume])
def main():
time = str(datetime.now().time())[:5]
if time == "6:50":
exit()
if time[3:] == "0":
get_data()
if time[3:] == "15":
get_data()
if time[3:] == "30":
get_data()
if time[3:] == "45":
get_data()
sleep(60)
while True:
main()
Upvotes: 1
Views: 1218
Reputation: 1222
Just open the file and then save the data frame as a CSV file for example
data = {'column': 'row1',
'column2': 'row1',
'column3' : 'row1',
'day': 'day',
'hour' : 'hour',
}
df=pd.DataFrame(data)
df.to_csv('twee.csv',index=False)
Upvotes: 1
Reputation: 159
I have never used the module csv in Python, but I have some experience with Pandas. You can do
import pandas as pd
then you can read the file with dataset=pd.read_csv(filename)
You can do all the manipulation on your dataset and then save it with dataset.to_csv(output_filename)
Upvotes: 0
Reputation: 1801
every time you open a file with mode "w" you completely overwrite it. Might it be that you want to use "a" to append instead? https://docs.python.org/3/tutorial/inputoutput.html#reading-and-writing-files
Upvotes: 0