Python 输出为列,同时添加逗号 (csv)
Python output as columns, also adding comma's (csv)
目前我正在使用IBAPI请求历史数据。
我在向数据添加 headers 时遇到了一些问题。我目前收到的输出是:
Ticker; Date; None; Time; Open; High; Low; Close; Volume AAPL ; 20190507; ; 10:19:00 ; 207.87 ; 207.87 ; 207.87 ; 207.87 ; 1
但我希望输出为
Symbol; Date; None; Time; Open; High; low; Close; Volume
AAPL ; 20190507; ; 16:20:00 ; 205.25 ; 205.38 ; 205.11 ; 205.35 ; 451
我目前使用的代码是:
from ibapi.client import EClient
from ibapi.wrapper import EWrapper
from ibapi.contract import Contract
def print_to_file(*args):
with open('text.txt', 'a') as fh:
fh.write(' '.join(map(str,args)))
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}"
print(Layout.format("Ticker;", "Date;", "None;", "Time;", "Open;", "High;", "Low;", "Close;", "Volume "))
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()
谁能帮我解决这个问题?
以下代码可以帮助您在 csv 文件中逐行写入值。
def header():
h1 = ['col1']
h2 = ['col2']
h3 = ['col3']
h4 = ['col4']
rows = zip(h1, h2, h3, h4)
with open('text.txt', 'a') as f:
wr = csv.writer(f)
for row in rows:
wr.writerow(row)
f.close()
根据问题的变化,建议:
def print_to_file(*args):
with open('text.txt', 'a') as fh:
fh.write(' '.join(map(str,args)))
fh.write('\n')
fh.close()
目前我正在使用IBAPI请求历史数据。
我在向数据添加 headers 时遇到了一些问题。我目前收到的输出是:
Ticker; Date; None; Time; Open; High; Low; Close; Volume AAPL ; 20190507; ; 10:19:00 ; 207.87 ; 207.87 ; 207.87 ; 207.87 ; 1
但我希望输出为
Symbol; Date; None; Time; Open; High; low; Close; Volume
AAPL ; 20190507; ; 16:20:00 ; 205.25 ; 205.38 ; 205.11 ; 205.35 ; 451
我目前使用的代码是:
from ibapi.client import EClient
from ibapi.wrapper import EWrapper
from ibapi.contract import Contract
def print_to_file(*args):
with open('text.txt', 'a') as fh:
fh.write(' '.join(map(str,args)))
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}"
print(Layout.format("Ticker;", "Date;", "None;", "Time;", "Open;", "High;", "Low;", "Close;", "Volume "))
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()
谁能帮我解决这个问题?
以下代码可以帮助您在 csv 文件中逐行写入值。
def header():
h1 = ['col1']
h2 = ['col2']
h3 = ['col3']
h4 = ['col4']
rows = zip(h1, h2, h3, h4)
with open('text.txt', 'a') as f:
wr = csv.writer(f)
for row in rows:
wr.writerow(row)
f.close()
根据问题的变化,建议:
def print_to_file(*args):
with open('text.txt', 'a') as fh:
fh.write(' '.join(map(str,args)))
fh.write('\n')
fh.close()