将数组字符串输入更改为整数输入不起作用
Changing array string inputs to integer inputs not working
对于一个使用 Cryptotrading 的项目,我正在尝试制作我自己的扫描仪。首先,我收集数据并将它们附加到列表中。然后我将列表转换为 np.array。该数组然后将字符串作为输入,但我需要将它们更改为整数。当我打印 np_closelist 时,它给出了所有需要的值,但在一个带有字符串的数组中。当我尝试打印 b(因此将 np_closelist 转换为整数)时,它不打印任何内容。当我在具有简单值的不同文件中尝试此方法时,它确实有效。谁能帮我弄清楚如何解决这个问题?
提前致谢!
import numpy as np
import talib
import websocket, json, pprint
socket = "wss://stream.binance.com:9443/ws/btcusdt@kline_1m"
closelist = []
def on_open(ws):
print('open')
def on_close(ws):
print('close')
def on_message(ws, message):
global closelist
json_message = json.loads(message)
# pprint.pprint(json_message)
candle = json_message['k']
is_candle_closed = candle['x']
close = candle['c']
high = candle['h']
low = candle['l']
if is_candle_closed:
closelist.append(format(float(close)))
closelist.append(format(float(high)))
closelist.append(format(float(low)))
np_closelist = np.array(closelist)
b = np.array([int(i) for i in np_closelist])
print(b)
print(type(b))
print(type(b[1]))
ws = websocket.WebSocketApp(socket, on_open=on_open, on_message=on_message, on_close=on_close)
ws.run_forever()
为什么要将浮点数格式化为字符串?
如果你这样做:
if is_candle_closed:
closelist.append(float(close))
closelist.append(float(high))
closelist.append(float(low))
np_closelist = np.array(closelist)
b = np.array([int(i) for i in np_closelist])
print(b)
print(type(b))
print(type(b[1]))
它应该会产生预期的结果。如果你先用 float cast 将 float 包裹在里面,它也应该有效,比如
b = np.array([int(float(i)) for i in np_closelist])
尝试将 float 字符串对象直接转换为 int 时出现问题,这通常会引发类似
的错误
ValueError: invalid literal for int() with base 10: '3.0'
对于一个使用 Cryptotrading 的项目,我正在尝试制作我自己的扫描仪。首先,我收集数据并将它们附加到列表中。然后我将列表转换为 np.array。该数组然后将字符串作为输入,但我需要将它们更改为整数。当我打印 np_closelist 时,它给出了所有需要的值,但在一个带有字符串的数组中。当我尝试打印 b(因此将 np_closelist 转换为整数)时,它不打印任何内容。当我在具有简单值的不同文件中尝试此方法时,它确实有效。谁能帮我弄清楚如何解决这个问题? 提前致谢!
import numpy as np
import talib
import websocket, json, pprint
socket = "wss://stream.binance.com:9443/ws/btcusdt@kline_1m"
closelist = []
def on_open(ws):
print('open')
def on_close(ws):
print('close')
def on_message(ws, message):
global closelist
json_message = json.loads(message)
# pprint.pprint(json_message)
candle = json_message['k']
is_candle_closed = candle['x']
close = candle['c']
high = candle['h']
low = candle['l']
if is_candle_closed:
closelist.append(format(float(close)))
closelist.append(format(float(high)))
closelist.append(format(float(low)))
np_closelist = np.array(closelist)
b = np.array([int(i) for i in np_closelist])
print(b)
print(type(b))
print(type(b[1]))
ws = websocket.WebSocketApp(socket, on_open=on_open, on_message=on_message, on_close=on_close)
ws.run_forever()
为什么要将浮点数格式化为字符串? 如果你这样做:
if is_candle_closed:
closelist.append(float(close))
closelist.append(float(high))
closelist.append(float(low))
np_closelist = np.array(closelist)
b = np.array([int(i) for i in np_closelist])
print(b)
print(type(b))
print(type(b[1]))
它应该会产生预期的结果。如果你先用 float cast 将 float 包裹在里面,它也应该有效,比如
b = np.array([int(float(i)) for i in np_closelist])
尝试将 float 字符串对象直接转换为 int 时出现问题,这通常会引发类似
的错误ValueError: invalid literal for int() with base 10: '3.0'