从tushare获取股票列表保存到csv,从csv中读出得到所有股票列表
时间: 2024-01-23 21:02:03 浏览: 115
好的,我来为您解答这个问题。
首先,您需要安装tushare库。您可以在命令行中输入以下命令来安装tushare:
```
pip install tushare
```
接下来,您可以使用以下代码从tushare获取股票列表并将其保存到csv文件中:
```python
import tushare as ts
# 获取股票列表
stock_list = ts.get_stock_basics()
# 将股票列表保存到csv文件中
stock_list.to_csv('stock_list.csv', encoding='utf-8')
```
上述代码将股票列表保存到名为“stock_list.csv”的文件中。
最后,您可以使用以下代码从csv文件中读取股票列表:
```python
import pandas as pd
# 从csv文件中读取股票列表
stock_list = pd.read_csv('stock_list.csv', index_col=0, dtype={'code': str})
```
上述代码将从名为“stock_list.csv”的文件中读取股票列表,并将其存储在名为“stock_list”的DataFrame中。
希望这可以帮助您回答您的问题!
相关问题
使用stock_basic获取股票信息保存到csv,从csv中读出到df,从df中提取股票列表
好的,我来为您解答这个问题。
首先,您需要安装tushare库。您可以在命令行中输入以下命令来安装tushare:
```
pip install tushare
```
接下来,您可以使用以下代码从tushare获取股票信息并将其保存到csv文件中:
```python
import tushare as ts
# 获取股票列表
stock_list = ts.get_stock_basics()
# 将股票信息保存到csv文件中
stock_list.to_csv('stock_info.csv', encoding='utf-8')
```
上述代码将股票信息保存到名为“stock_info.csv”的文件中。
最后,您可以使用以下代码从csv文件中读取股票信息,并从中提取股票列表:
```python
import pandas as pd
# 从csv文件中读取股票信息
stock_info = pd.read_csv('stock_info.csv', index_col=0, dtype={'code': str})
# 提取股票列表
stock_list = stock_info.index.tolist()
```
上述代码将从名为“stock_info.csv”的文件中读取股票信息,并将其存储在名为“stock_info”的DataFrame中。然后,它会使用DataFrame的“index.tolist()”方法提取股票列表,将其存储在名为“stock_list”的列表中。
希望这可以帮助您回答您的问题!
编写Python代码从Tushare获得个股数据并保存csv文件
好的,下面是Python代码,可以通过Tushare获取个股数据并将其保存为CSV文件。
```python
import tushare as ts
# 设置Tushare的token
ts.set_token('YOUR_TOKEN_HERE')
# 初始化pro接口
pro = ts.pro_api()
# 获取个股数据
df = pro.daily(ts_code='股票代码', start_date='开始日期', end_date='结束日期')
# 保存CSV文件
df.to_csv('股票代码.csv', index=False)
```
将上述代码中的 `YOUR_TOKEN_HERE` 替换为你在Tushare注册时获得的API令牌,将 `股票代码` 替换为你要获取数据的股票代码,将 `开始日期` 和 `结束日期` 分别替换为你要获取数据的时间范围。
运行代码后,将会在当前工作目录下生成一个名为 `股票代码.csv` 的CSV文件,其中包含了所需的个股数据。
阅读全文
相关推荐















