0

How can I convert UNIX timestamp into UTC timestamp and export data with UTC timestamp to csv in python? Here is my code:

from csv import DictWriter
orders = client.get_my_sales(symbol='AI95')
with open ('testcsv.csv', 'w') as outfile:
    writer = DictWriter (outfile, ('time', 'symbol','orderId','price','qty'))
    writer.writeheader()
    writer.writerows (orders)

And this is the output of print(client.get_my_sales(symbol='AI95'))

[{'orderId': 22153,   'price': '500.30000000',   'qty': '8.05000000','qty': '1910.26500000',   'symbol': 'AI95',   'time': 1657118024716},

{'orderId': 22153,   'price': '600.30000000',   'qty': '10.05000000', 'qty': '2040.26500000',   'symbol': 'AI95',   'time': 1657118030000}]
Alex
  • 3
  • 2

1 Answers1

0

You can use this function:

def convert_to_utc_from_unix(unix_time):
    return time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime(unix_time))

to convert an UNIX timestamp into UTC in python. It returns time in UTC like this: 2022-07-06 17:42:14, you can change the formatting however you like for example instead of - you can use /. Just remember to import time.

Then after converting to UTC, get the array and replace the UNIX time with UTC and write it to your .csv file.

nnaem
  • 177
  • 1
  • 2
  • 12