Python .replace() 函数似乎没有做任何事情
Python .replace() function doesn't seem to do anything
我正在尝试修改 table 我从 Yahoo Finance 中收集到的包含过去 5 年股息的内容。
此 table 的示例行是:"1.50 Dividend"
。因为我想用这个 table 进行计算,所以我只需要浮点数形式的数字。所以我使用 .replace("Dividend, "")
函数删除文本部分,以便 float()
函数能够转换它。
执行此操作时,我总是会遇到以下错误:
TypeError: could not convert string to float: '1.50 Dividend'
似乎 .replace("Dividend", "")
根本没有做任何事情。
这是代码:
#Fetching Yahoo Finance dividend table (5y)
url = f"https://finance.yahoo.com/quote/{symbol}/history?period1=1491782400&period2=1649548800&interval=capitalGain%7Cdiv%7Csplit&filter=div&frequency=1d&includeAdjustedClose=true"
header = { "User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.75 Safari/537.36", "X-Requested-With": "XMLHttpRequest" }
r = requests.get(url, headers=header)
dfs = pd.read_html(r.text)
dividends = dfs[0]["Dividends"]
dividends = dividends.head(5)
counter = 0
if len(dividends) > 2:
while counter != len(dividends):
dividend_string = dividends[counter]
dividends[counter] = float(dividend_string.replace(" Dividend", ""))
counter += 1
sigma = dividends.std()
else:
sigma = 0
sigma_ratio = sigma / dividends.mean()
return sigma_ratio
我试过在一个小的测试脚本中使用 .replace()
,它似乎工作得很好。我已经束手无策了。
提前感谢您的回答!
问题不在 .replace()
,而在 dividends.std()
。
将 sigma = dividends.std()
移到 while 循环之外,现在您只是从第一个元素中删除“股息”,然后尝试调用 dividends.std()
但数组的其余部分仍然包含股息。
#Fetching Yahoo Finance dividend table (5y)
url = f"https://finance.yahoo.com/quote/{symbol}/history?period1=1491782400&period2=1649548800&interval=capitalGain%7Cdiv%7Csplit&filter=div&frequency=1d&includeAdjustedClose=true"
header = { "User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.75 Safari/537.36", "X-Requested-With": "XMLHttpRequest" }
r = requests.get(url, headers=header)
dfs = pd.read_html(r.text)
dividends = dfs[0]["Dividends"]
dividends = dividends.head(5)
counter = 0
if len(dividends) > 2:
while counter != len(dividends):
dividend_string = dividends[counter]
dividends[counter] = float(dividend_string.replace(" Dividend", ""))
counter += 1
sigma = dividends.std() # <-----
else:
sigma = 0
sigma_ratio = sigma / dividends.mean()
return sigma_ratio
我正在尝试修改 table 我从 Yahoo Finance 中收集到的包含过去 5 年股息的内容。
此 table 的示例行是:"1.50 Dividend"
。因为我想用这个 table 进行计算,所以我只需要浮点数形式的数字。所以我使用 .replace("Dividend, "")
函数删除文本部分,以便 float()
函数能够转换它。
执行此操作时,我总是会遇到以下错误:
TypeError: could not convert string to float: '1.50 Dividend'
似乎 .replace("Dividend", "")
根本没有做任何事情。
这是代码:
#Fetching Yahoo Finance dividend table (5y)
url = f"https://finance.yahoo.com/quote/{symbol}/history?period1=1491782400&period2=1649548800&interval=capitalGain%7Cdiv%7Csplit&filter=div&frequency=1d&includeAdjustedClose=true"
header = { "User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.75 Safari/537.36", "X-Requested-With": "XMLHttpRequest" }
r = requests.get(url, headers=header)
dfs = pd.read_html(r.text)
dividends = dfs[0]["Dividends"]
dividends = dividends.head(5)
counter = 0
if len(dividends) > 2:
while counter != len(dividends):
dividend_string = dividends[counter]
dividends[counter] = float(dividend_string.replace(" Dividend", ""))
counter += 1
sigma = dividends.std()
else:
sigma = 0
sigma_ratio = sigma / dividends.mean()
return sigma_ratio
我试过在一个小的测试脚本中使用 .replace()
,它似乎工作得很好。我已经束手无策了。
提前感谢您的回答!
问题不在 .replace()
,而在 dividends.std()
。
将 sigma = dividends.std()
移到 while 循环之外,现在您只是从第一个元素中删除“股息”,然后尝试调用 dividends.std()
但数组的其余部分仍然包含股息。
#Fetching Yahoo Finance dividend table (5y)
url = f"https://finance.yahoo.com/quote/{symbol}/history?period1=1491782400&period2=1649548800&interval=capitalGain%7Cdiv%7Csplit&filter=div&frequency=1d&includeAdjustedClose=true"
header = { "User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.75 Safari/537.36", "X-Requested-With": "XMLHttpRequest" }
r = requests.get(url, headers=header)
dfs = pd.read_html(r.text)
dividends = dfs[0]["Dividends"]
dividends = dividends.head(5)
counter = 0
if len(dividends) > 2:
while counter != len(dividends):
dividend_string = dividends[counter]
dividends[counter] = float(dividend_string.replace(" Dividend", ""))
counter += 1
sigma = dividends.std() # <-----
else:
sigma = 0
sigma_ratio = sigma / dividends.mean()
return sigma_ratio