pip install tushare ;
stock2json.py
# coding: utf-8
import os, sys
import pandas as pd
import tushare as ts
if len(sys.argv) ==2:
code = sys.argv[1]
else:
print('usage: python stock2json.py stockcode ')
sys.exit(1)
if len(code) !=6:
print('stock code length: 6')
sys.exit(2)
# help(ts.get_k_data) 了解参数
k_data = ts.get_k_data(code)
# 取样 2018年以后的收盘价
df = k_data[ k_data['date'] >'2018-01-01']
f2 = code +'.json'
fp2 = open(f2,'w')
fp2.write('[')
i = 0
n = df.shape[0] # 行数
for idx,row in df.iterrows():
i +=1
date,open,close,high,low,volume = row[0:6]
if i < n:
ss = "['%s',%s,%s,%s,%s],\n" % (date,open,close,low,high)
else:
ss = "['%s',%s,%s,%s,%s]]" % (date,open,close,low,high)
fp2.write(ss)
#
fp2.close()
print(" {0} rows".format(i))
运行 python stock2json.py 600030