2
I’m new to Python and I have a question I’ve been trying to figure out for a long time.
I’m creating a tick analysis program from an investment site, and I want to extract the value of a certain string. I even managed to get to the character I want to extract, but I cannot return it to the main.
Below is the main code for you to understand what I want to do:
import websocket
import json
def on_open(ws):
json_data = json.dumps({'ticks':'R_100'})
ws.send(json_data)
def on_message(ws, message):
print('ticks update: %s' % message)
if __name__ == "__main__":
apiUrl = "wss://ws.binaryws.com/websockets/v3?app_id=1089"
ws = websocket.WebSocketApp(apiUrl, on_message = on_message, on_open = on_open)
ws.run_forever()
I want to extract from the function the numbers highlighted in yellow, I took from IDLE:
I even managed to extract the number I want, as image below, and I can print it in function:
import websocket
import json
def on_open(ws):
json_data = json.dumps({'ticks': 'R_100'})
ws.send(json_data)
def on_message(ws, message):
a=""
a=message[235]
print(message)
print('a = ', a)
if __name__ == "__main__":
apiUrl = "wss://ws.binaryws.com/websockets/v3?app_id=1089"
ws = websocket.WebSocketApp(apiUrl, on_message = on_message, on_open = on_open)
ws.run_forever()
But I can’t pull out the function because I want to work with this number on main, whereas in function it continues with the same number and not to use the if.
import websocket
import json
def on_open(ws):
json_data = json.dumps({'ticks': 'R_100'})
ws.send(json_data)
def on_message(ws, message):
a=""
a=message[235]
return a
if __name__ == "__main__":
apiUrl = "wss://ws.binaryws.com/websockets/v3?app_id=1089"
ws = websocket.WebSocketApp(apiUrl, on_message = on_message, on_open = on_open)
tick=""
tick=on_message
print(tick)
ws.run_forever()
IDLE prints this instead of printing the desired number: <Function on_message at 0x00000195A1D4C310>
Can someone help me?
Someone can help?
– Luiz Mendes
Hi Luiz, there is no way you can return the value of
a
function, but there are other things you can do. I can help you with that, but what you intend to do with that character in the main?– Flavio Moraes