[英]Reformatting Output Python
Currently I am trying to request data from the IB API, but I have a small formatting issue. 目前我正在尝试从IB API请求数据,但我有一个小的格式问题。
The API gives me the following output: API为我提供了以下输出:
AAPL; 20190507 16:20:00; price; price; price; price; number
I would like the data to return as: 我希望数据返回为:
AAPL; 20190507; 16:20:00; price; price; price; price; number
I am using the following code 我使用以下代码
from ibapi.client import EClient
from ibapi.wrapper import EWrapper
from ibapi.contract import Contract
class TestApp(EWrapper, EClient):
def __init__(self):
EClient.__init__(self, self)
def error(self, reqId, errorCode, errorString):
print("error: ", reqId, " ", errorCode, " ", errorString)
def historicalData(self, reqId, bar):
print("AAPL", ";", bar.date, ";", bar.open, ";", bar.high, ";", bar.low, ";", bar.close, ";", bar.volume)
def main():
app = TestApp()
app.connect("127.0.0.1", 7497, 0)
contract = Contract ()
contract.symbol = "AAPL"
contract.secType = "STK"
contract.exchange = "SMART"
contract.currency = "USD"
contract.primaryExchange = "NASDAQ"
app.reqHistoricalData(0, contract, "", "1 D", "1 min", "TRADES", 0, 1, False, [])
app.run()
if __name__ == "__main__":
main()
bar.date in this case gives me the date and time 在这种情况下,bar.date给了我日期和时间
print("AAPL", ";", bar.date, ";", bar.open, ";", bar.high, ";", bar.low, ";", bar.close, ";", bar.volume)
Could anyone help me out with this? 任何人都可以帮我解决这个问题吗?
Try this: 尝试这个:
'; '.join(bar.date.split(' '))
Or: 要么:
bar.date.replace(' ', '; ')
Both of these will replace the space in the date/time output with a semicolon and space. 这两个都将用分号和空格替换日期/时间输出中的空格。
As I understood, bar.date
contains "20190507 16:20:00"
. 据我了解,
bar.date
包含"20190507 16:20:00"
。
So you can replace bar.date
with "; ".join(bar.date.split(" "))
in last print()
you've provided. 所以你可以在你提供的最后一个
print()
中用"; ".join(bar.date.split(" "))
替换bar.date
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.