[英]Error when I try to save pandas as excel file in python
I´m using the following code to get data from websocket and save as excel file, but when I try to open the file, the data is unorganized我正在使用以下代码从 websocket 获取数据并保存为 excel 文件,但是当我尝试打开文件时,数据是杂乱无章的
import websocket
import json
import pandas as pd
df = pd.DataFrame(columns=['timestamp', 'symbol', 'side','size', 'price', 'tickDirection', 'trdMatchID', 'grossValue', 'homeNotional', 'foreignNotional'])
def on_open(ws):
print("opened")
auth_data = {
'op': 'subscribe',
'args': ['trade:XBTUSD']
}
ws.send(json.dumps(auth_data))
def on_message(ws, message):
message = json.loads(message)
print('\n',message)
global df
df = df.append(message, ignore_index=True)
df.to_excel (r'D:\python\00trace.xlsx', header=True)
def on_close(ws):
print("closed connection")
socket = "wss://www.bitmex.com/realtime"
ws = websocket.WebSocketApp(socket,
on_open=on_open,
on_message=on_message,
on_close=on_close)
ws.run_forever()
and the excel that I get is:我得到的 excel 是:
It seems like your message
does not have the same columns as your df
.您的message
似乎与您的df
没有相同的列。 You probably need to cast the message
JSON object into a DataFrame then rename the columns such that it matches the columns in df
.您可能需要将message
JSON object 转换为 DataFrame 然后重命名列,使其与df
中的列匹配。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.