Python代码正在保存单独的文本文件,我们可以使用pandas.dataframe()以整洁的格式保存在excel中。我尝试了熊猫,但是我的excel文件格式不正确,很乱!
import datetime
import time
from kiteconnect import KiteConnect
tdelta0=datetime.timedelta(days=30)
tdelta=datetime.timedelta(days=29)
ak='xxxxxxxxxxxxxxxxxx'
asecret='xxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
kite = KiteConnect(api_key=ak)
request_tkn = input("[*] Enter Your Request Token Here : ");
data = kite.generate_session(request_tkn, api_secret=asecret)
kite.set_access_token(data["access_token"])
interval = "day"
savedata={}
def get_historical_data(instrument_token):
return kite.historical(instrument_token, from_date, to_date, interval)
trd_portfolio={5633: 'ACC',6401: 'ADANIENT',3861249: 'ADANIPORTS',2079745: 'AJANTPHARM'}
for token in trd_portfolio:
f= open(str(trd_portfolio[token])+".txt","w+")
from_date=datetime.date(2015,2,1)
to_date=from_date+tdelta
for x in range(1,45):
print(x)
print("\n",from_date)
print(to_date)
records = kite.historical_data(token, from_date, to_date, interval)
f.write(str(records))
for x in records:
print(x)
from_date=from_date+tdelta0
to_date=from_date+tdelta
print(token)
答案 0 :(得分:0)
IIUC,
data = """
'open':31427, 'high':31469, 'open':31427, 'high':31469, ,'open':3145427, 'high':31469, 'open':31427, 'high':31469, 'open':31427, 'high':314569"""
我们只需要在读取的csv中设置正确的换行符和定界符
from io import StringIO
import pandas as pd
df = pd.read_csv(StringIO(data),sep=':',lineterminator=',',header=None)
df.columns = ['status','price']
print(df)
Status Price
0 'open' 31427
1 'high' 31469
2 'open' 31427
3 'high' 31469
4 'open' 3145427
5 'high' 31469
6 'open' 31427
7 'high' 31469
8 'open' 31427
9 'high' 314569