文件突然写入 1 行数据而不是整个请求
File suddenly writes 1 row of data instead of whole entire request
我正在使用 IBAPI 收集历史股票报价,目前,我正在使用此代码来实现:
from ibapi.client import EClient
from ibapi.wrapper import EWrapper
from ibapi.contract import Contract
def print_to_file(*args):
with open('text2.txt', 'w') as fh:
fh.write(' '.join(map(str,args)))
fh.write('\n')
fh.close()
print = print_to_file
class TestApp(EWrapper, EClient):
def __init__(self):
EClient.__init__(self, self)
Layout = "{!s:1} {!s:2} {!s:3} {!s:4} {!s:5} {!s:6} {!s:7} {!s:8} {!s:8} '\n'"
print(Layout.format("Ticker;", "Date;", "None;", "Time;", "Open;", "High;", "Low;", "Close;", "Volume", '\n'))
def historicalData(self, reqId, bar):
print("AAPL", ";", bar.date.replace(' ', '; '), ";", 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()
这段代码起初运行良好,但突然只返回 1 行文本,而不是请求附带的所有行。
这里有人可以帮我找出问题所在吗?
应该写入文本文件的代码是:
def print_to_file(*args):
with open('text2.txt', 'w') as fh:
fh.write(' '.join(map(str,args)))
fh.write('\n')
fh.close()
print = print_to_file
如果我让代码写入终端而不是文件,我会得到整个请求,而不仅仅是 1 行。
谢谢!
文件以 'w' 模式打开,因此函数会在每次打开时截断文件。
应该使用模式 'a'。
https://docs.python.org/3/library/functions.html#open
'w' open for writing, truncating the file first
'a' open for writing, appending to the end of the file if it exists
我正在使用 IBAPI 收集历史股票报价,目前,我正在使用此代码来实现:
from ibapi.client import EClient
from ibapi.wrapper import EWrapper
from ibapi.contract import Contract
def print_to_file(*args):
with open('text2.txt', 'w') as fh:
fh.write(' '.join(map(str,args)))
fh.write('\n')
fh.close()
print = print_to_file
class TestApp(EWrapper, EClient):
def __init__(self):
EClient.__init__(self, self)
Layout = "{!s:1} {!s:2} {!s:3} {!s:4} {!s:5} {!s:6} {!s:7} {!s:8} {!s:8} '\n'"
print(Layout.format("Ticker;", "Date;", "None;", "Time;", "Open;", "High;", "Low;", "Close;", "Volume", '\n'))
def historicalData(self, reqId, bar):
print("AAPL", ";", bar.date.replace(' ', '; '), ";", 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()
这段代码起初运行良好,但突然只返回 1 行文本,而不是请求附带的所有行。
这里有人可以帮我找出问题所在吗?
应该写入文本文件的代码是:
def print_to_file(*args):
with open('text2.txt', 'w') as fh:
fh.write(' '.join(map(str,args)))
fh.write('\n')
fh.close()
print = print_to_file
如果我让代码写入终端而不是文件,我会得到整个请求,而不仅仅是 1 行。
谢谢!
文件以 'w' 模式打开,因此函数会在每次打开时截断文件。 应该使用模式 'a'。
https://docs.python.org/3/library/functions.html#open
'w' open for writing, truncating the file first
'a' open for writing, appending to the end of the file if it exists