LUCIT-Systems-and-Development / unicorn-binance-websocket-api

A Python SDK by LUCIT to use the Binance Websocket API`s (com+testnet, com-margin+testnet, com-isolated_margin+testnet, com-futures+testnet, com-coin_futures, us, tr, dex/chain+testnet) in a simple, fast, flexible, robust and fully-featured way.
https://unicorn-binance-websocket-api.docs.lucit.tech/
Other
683 stars 164 forks source link

Write websocket user_data output to csv #100

Closed CryptoFuturesAU closed 4 years ago

CryptoFuturesAU commented 4 years ago

Hello,

I am trying to write terminal output to csv. Currently my code looks like this:

def print_stream_data_from_stream_buffer(binance_websocket_api_manager): while True: if binance_websocket_api_manager.is_manager_stopping(): exit(0) oldest_stream_data_from_stream_buffer = binance_websocket_api_manager.pop_stream_data_from_stream_buffer() if oldest_stream_data_from_stream_buffer is False: time.sleep(0.01) else: print(oldest_stream_data_from_stream_buffer) with open('tradehistory.csv', 'w') as f: w = csv.DictWriter(f, oldest_stream_data_from_stream_buffer) w.writerow(oldest_stream_data_from_stream_buffer)**

I get this error output:

Traceback (most recent call last): File "C:\ProgramData\Anaconda3\lib\threading.py", line 932, in _bootstrap_inner self.run() File "C:\ProgramData\Anaconda3\lib\threading.py", line 870, in run self._target(*self._args, **self._kwargs) File "C:\Users\asus\Documents\Personal\Corporate\Python\bad_apple.py", line 34, in print_stream_data_from_stream_buffer w.writerow(oldest_stream_data_from_stream_buffer) File "C:\ProgramData\Anaconda3\lib\csv.py", line 154, in writerow return self.writer.writerow(self._dict_to_list(rowdict)) File "C:\ProgramData\Anaconda3\lib\csv.py", line 147, in _dict_to_list wrong_fields = rowdict.keys() - self.fieldnames AttributeError: 'str' object has no attribute 'keys'

Very new to coding and appreciate any advice given.

oliver-zehentleitner commented 4 years ago

Would be cool if you use the insert code button <>

I GUESS you need to use DictWriter with a dict instead of string variable!

CryptoFuturesAU commented 4 years ago

Could you please write an example? Thank you

oliver-zehentleitner commented 4 years ago

https://github.com/oliver-zehentleitner/unicorn-binance-websocket-api#convert-received-raw-webstream-data-into-well-formed-python-dictionaries-with-unicornfy