我正在尝试从此处运行代码来下载所有 S&P 500 股票:
https://pythonprogramming.net/sp500-company-price-data-python-programming-for-finance/
import bs4 as bs
import datetime as dt
import os
import pandas_datareader.data as web
import pickle
import requests
def save_sp500_tickers():
resp = requests.get('http://en.wikipedia.org/wiki/List_of_S%26P_500_companies')
soup = bs.BeautifulSoup(resp.text, 'lxml')
table = soup.find('table', {'class': 'wikitable sortable'})
tickers = []
for row in table.findAll('tr')[1:]:
ticker = row.findAll('td')[0].text
tickers.append(ticker)
with open("sp500tickers.pickle", "wb") as f:
pickle.dump(tickers, f)
return tickers
# save_sp500_tickers()
def get_data_from_yahoo(reload_sp500=False):
if reload_sp500:
tickers = save_sp500_tickers()
else:
with open("sp500tickers.pickle", "rb") as f:
tickers = pickle.load(f)
if not os.path.exists('stock_dfs'):
os.makedirs('stock_dfs') …Run Code Online (Sandbox Code Playgroud) 我试图在文件夹中的所有文本文件上执行python脚本:
for fi in sys.argv[1:]:
Run Code Online (Sandbox Code Playgroud)
我收到以下错误
-bash: /usr/bin/python: Argument list too long
Run Code Online (Sandbox Code Playgroud)
我称之为Python函数的方式如下:
python functionName.py *.txt
Run Code Online (Sandbox Code Playgroud)
该文件夹有大约9000个文件.有没有办法运行此功能,而不必将我的数据拆分到更多的文件夹等?拆分文件不太实用,因为我将来必须在更多文件中执行该功能...谢谢
编辑:根据选定的正确回复和回复者(Charles Duffy)的评论,对我有用的是:
printf '%s\0' *.txt | xargs -0 python ./functionName.py
Run Code Online (Sandbox Code Playgroud)
因为我没有有效的shebang ..
我有一个 Python 数据框,如下所示:
year_2021 year_2020 year_2019 year_2018 year_2017 year_2016 year_2015
A 15.930541127542696 0.12659425148389353 37.859222886444584 0.7502044402344105 29.867551207184103 -2.62377211249297 -0.20709969350376067
AA 67.78493901734711 0.8676822974374843 7.609706605792995 -0.4184107467700045 -18.02591304685353 -1.2791578388405083 -51.821640736128046
AAL 61.46728433834196 -4.05834067688741 -45.78892961483622 1.4295670957092637 -11.699505205100467 1.1522856582117076 -39.40366183570082
Run Code Online (Sandbox Code Playgroud)
我试图获得每年最大的 n:
dataframe = yearly_returns.nlargest(5, "year_"+str(2021))
Run Code Online (Sandbox Code Playgroud)
这给了我错误TypeError: Column 'year_2021' has dtype object, cannot use method 'nlargest' with this dtype
我该如何解决这个问题?
I am trying to run the following code:
import cv2
import pytesseract
img = cv2.imread('/Users/user1/Desktop/folder1/pdf1.pdf')
text = pytesseract.image_to_string(img)
print(text)
Run Code Online (Sandbox Code Playgroud)
which gives me the following error:
Traceback (most recent call last):
File "/Users/user1/PycharmProjects/project1/python_file.py", line 5, in <module>
text = pytesseract.image_to_string(img)
File "/Users/user1/PycharmProjects/project1/venv/lib/python3.8/site-packages/pytesseract/pytesseract.py", line 346, in image_to_string
return {
File "/Users/user1/PycharmProjects/project1/venv/lib/python3.8/site-packages/pytesseract/pytesseract.py", line 349, in <lambda>
Output.STRING: lambda: run_and_get_output(*args),
File "/Users/user1/PycharmProjects/project1/venv/lib/python3.8/site-packages/pytesseract/pytesseract.py", line 249, in run_and_get_output
with save(image) as (temp_name, input_filename):
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/contextlib.py", line 113, in __enter__
return next(self.gen)
File "/Users/user1/PycharmProjects/project1/venv/lib/python3.8/site-packages/pytesseract/pytesseract.py", line 172, in …Run Code Online (Sandbox Code Playgroud)