Johan
Johan

Reputation: 23

Python: Can't save to CSV

I have a script that pulls information from an API and is supposed to write it to a CSV file which I have in the same directory. For some reason, all the code seems to execute fine but the CSV never actually contains any data. Am I missing something crucial? I've checked the documentation for the CSV module but can't find anything.

Here is my code:

import time, json, requests, csv

with open('data.csv', 'w') as csvfile:
    fieldnames = ['time','last','vwap','high','low','open','vol']
    writer = csv.DictWriter(csvfile, fieldnames=fieldnames)

    writer.writeheader()

    def btstampTime():
        bitStampTick = requests.get('https://www.bitstamp.net/api/ticker/')
        return bitStampTick.json()['timestamp']

    def btstampLast():
        bitStampTick = requests.get('https://www.bitstamp.net/api/ticker/')
        return bitStampTick.json()['last']

    def btstampVWAP():
        bitStampTick = requests.get('https://www.bitstamp.net/api/ticker/')
        return bitStampTick.json()['vwap']

    def btstampHigh():
        bitStampTick = requests.get('https://www.bitstamp.net/api/ticker/')
        return bitStampTick.json()['high']

    def btstampLow():
        bitStampTick = requests.get('https://www.bitstamp.net/api/ticker/')
        return bitStampTick.json()['low']

    def btstampOpen():
        bitStampTick = requests.get('https://www.bitstamp.net/api/ticker/')
        return bitStampTick.json()['open']

    def btstampVol():
        bitStampTick = requests.get('https://www.bitstamp.net/api/ticker/')
        return bitStampTick.json()['volume']


    while True:
        timestamp = btstampTime()
        last = float(btstampLast())
        vwap = float(btstampVWAP())
        high = float(btstampHigh())
        low = float(btstampLow())
        open = float(btstampOpen())
        vol = float(btstampVol())

        writer.writerow({'time': timestamp,
                         'last': last,
                         'vwap': vwap,
                         'high': high,
                         'low': low,
                         'open': open,
                         'vol': vol})

        print('Time: ', timestamp)
        print('Last =', last)
        print('VWAP =', vwap)
        print('High =', high)
        print('Low =', low)
        print('Open =', open)
        print('Volume =', vol)
        print('')

        time.sleep(60)

Upvotes: 2

Views: 772

Answers (2)

Denis Rasulev
Denis Rasulev

Reputation: 4069

Here is working (and optimized :)) solution. Bear in mind that info will be written to file, but it is possible that OS won't update file size visually in real time.

import os
import csv
import time
import requests

csv_file = open('data.csv', 'w')
field_names = ['time', 'last', 'vwap', 'high', 'low', 'open', 'vol']
writer = csv.DictWriter(csv_file, fieldnames=field_names)
writer.writeheader()

while True:
    info = requests.get('https://www.bitstamp.net/api/ticker/').json()
    writer.writerow({'time': info['timestamp'],
                     'last': info['last'],
                     'vwap': info['vwap'],
                     'high': info['high'],
                     'low' : info['low'],
                     'open': info['open'],
                     'vol' : info['volume']})
    csv_file.flush()
    os.fsync(csv_file.fileno())
    time.sleep(60)

    # this line is optional, you can delete it.
    print('Info appended.')

Upvotes: 2

Anton vBR
Anton vBR

Reputation: 18906

Using pandas:

import pandas as pd
import requests

d = requests.get('https://www.bitstamp.net/api/ticker/').json()
cols = ['timestamp', 'last', 'vwap', 'high', 'low', 'open', 'volume']
df = pd.DataFrame([d.get(i,'') for i in cols], cols).T
df.to_csv('output.csv', index=False)

Creates ouput.csv with:

timestamp,last,vwap,high,low,open,volume
1521332251,7831.18,8069.97,8356.40000000,7730.23000000,7860.83,11882.59781852

Upvotes: 0

Related Questions