初学python,不当之处,敬请指出二种方式取得的数据打印出来一样,但处理起来方式不能一样,摸索了很久,现在知其然不知其所以然
import time
import pandas as pd
import datetime
from sqlalchemy import create_engine
from mysql_tables_structure import Base
import pymysql
pymysql.install_as_MySQLdb()
#从数据库取列表# 创建数据库引擎
engine = create_engine("mysql://root:密码@127.0.0.1/数据库名?charset=utf8MB4")
conn = engine.connect()
# 创建mysql所有表结构
Base.metadata.create_all(engine)
#读取股票列表
codes =pd.read_sql('select * from 表名', engine)
#使用列表数据
for EachID,index in zip(codes['ts_code'],codes.index):
print(EachID)
print(codes.loc[index][2]) #name
......
#从Tushare取列表
# 连接 tushare
ts.set_token('你的token')
pro = ts.pro_api()
codes = get_stock_basic(pro, retry_count=3, pause=2)
for EachID,index in zip(codes['ts_code'],codes.index):
for EachID, name in zip(codes[0]['ts_code'], codes[0]['name']):
print(EachID)print(name)
def get_stock_basic(pro, retry_count=3, pause=2):
"""取得股票列表数据"""
frame = []
exc = ["SSE","SZSE"]
for ex in exc:
for status in ['L', 'D', 'P']:
for _ in range(retry_count):
try:
df = pro.stock_basic(exchange=ex, list_status=status,
fields='ts_code,symbol,name,area,industry,fullname,enname,market, \
exchange,curr_type,list_status,list_date,delist_date,is_hs')
except:
time.sleep(pause)
else:
frame.append(df)
# break
return frame