无法将 yfinance 中的数据保存到 CSV 文件中 [英] Can't save data from yfinance into a CSV file

查看:46
本文介绍了无法将 yfinance 中的数据保存到 CSV 文件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现了一个库,可以让我非常有效地从雅虎财经获取数据.这是一个很棒的图书馆.

I found library that allows me to get data from yahoo finance very efficiently. It's a wonderful library.

问题是,我无法将数据保存到 csv 文件中.

The problem is, I can't save the data into a csv file.

我已经尝试将数据转换为 Panda Dataframe,但我认为我做错了,我得到了一堆 'NaN's.

I've tried converting the data to a Panda Dataframe but I think I'm doing it incorrectly and I'm getting a bunch of 'NaN's.

我尝试使用 Numpy 直接保存到 csv 文件中,但这也不起作用.

I tried using Numpy to save directly into a csv file and that's not working either.

import yfinance as yf
import csv
import numpy as np

urls=[
'voo',
'msft'
    ]

for url in urls:
    tickerTag = yf.Ticker(url)

    print(tickerTag.actions)
    np.savetxt('DivGrabberTest.csv', tickerTag.actions, delimiter = '|')

我可以在控制台上打印数据,这很好.请帮我将其保存到 csv 中.谢谢!

I can print the data on console and it's fine. Please help me save it into a csv. Thank you!

推荐答案

如果您想将每个 url 的股票代码结果存储在不同的 csv 文件中,您可以这样做:

If you want to store the ticker results for each url in different csv files you can do:

for url in urls:
    tickerTag = yf.Ticker(url)
    tickerTag.actions.to_csv("tickertag{}.csv".format(url))

如果您希望它们都在同一个 csv 文件中,您可以这样做

if you want them all to be in the same csv file you can do

import pandas as pd
tickerlist = [yf.Ticker.url for url in urls]
pd.concat(tickerlist).to_csv("tickersconcat.csv")

这篇关于无法将 yfinance 中的数据保存到 CSV 文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆