1

Ok I am a python beginner who tries fetching data from iforge. However I get problem with timestamp when exporting to CSV. I think timestamp should look like this "2019-03-22 23:00:00" but instead I get 1553460483. Why is that and how to fix it so it becomes in correct format in the csv file?

# coding: utf-8
import json
import csv
import urllib.request
import datetime

data = json.load(request)

time = data[0]['timestamp']
price = data[0]['price']

data = json.load(request) contains this -

[{'symbol': 'EURUSD',
  'bid': 1.2345,
  'ask': 1.2399,
  'price': 1.2343,
  'timestamp': 1553460483}]

But since I was only interested in price and timestamp I did-

time = data[0]['timestamp']
price = data[0]['price']
myprice = {'Date':time,'price':price}

And then made csv from myprice....it works but I dont know if correct =) Now to problem -

How to fix timestamp to show up correctly in CSV?

1 Answers1

0

You would have to figure out what unit 'timestamp' is in. My guess would be seconds since a certain start date, so go for:

import pandas as pd
pd.to_datetime(1553460483, unit='s')

Out: Timestamp('2019-03-24 20:48:03')
Kai Aeberli
  • 1,189
  • 8
  • 21
  • Yes that did the trick with pandas! Many thanks! Any idea how to without pandas? –  Mar 24 '19 at 22:26
  • 1
    check this: https://stackoverflow.com/questions/9744775/how-to-convert-integer-timestamp-to-python-datetime – Kai Aeberli Mar 24 '19 at 22:29