[英]Reformatting Output Python
目前我正在嘗試從IB API請求數據,但我有一個小的格式問題。
API為我提供了以下輸出:
AAPL; 20190507 16:20:00; price; price; price; price; number
我希望數據返回為:
AAPL; 20190507; 16:20:00; price; price; price; price; number
我使用以下代碼
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給了我日期和時間
print("AAPL", ";", bar.date, ";", bar.open, ";", bar.high, ";", bar.low, ";", bar.close, ";", bar.volume)
任何人都可以幫我解決這個問題嗎?
嘗試這個:
'; '.join(bar.date.split(' '))
要么:
bar.date.replace(' ', '; ')
這兩個都將用分號和空格替換日期/時間輸出中的空格。
據我了解, bar.date
包含"20190507 16:20:00"
。
所以你可以在你提供的最后一個print()
中用"; ".join(bar.date.split(" "))
替換bar.date
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.