[英]Writing a pandas dataframe to csv
I want to write a pandas data frame to a CSV file. 我想将熊猫数据框写入CSV文件。 However, the last line of my code outputs following error.
但是,我的代码的最后一行输出以下错误。
'PRN' is the name of the ticker
“ PRN”是股票代码的名称
{FileNotFoundError}[Errno 2] No such file or directory: G:\\\\stock_data/daily/PRN.csv{FileNotFoundError} [错误2]没有这样的文件或目录:G:\\\\ stock_data / daily / PRN.csv
I already checked that the folder "G:\\stock_data\\daily" exists. 我已经检查了文件夹“ G:\\ stock_data \\ daily”的存在。
The issue seems that 'PRN' is a reserved name in windows for Printer. 问题似乎是“ PRN”是Windows中打印机的保留名称。 Is there a way that i can save a csv like PRN.csv?
有没有一种方法可以保存诸如PRN.csv的csv? https://superuser.com/questions/613313/why-cant-we-make-con-prn-null-folder-in-windows
https://superuser.com/questions/613313/why-cant-we-make-con-prn-null-folder-in-windows
data = None
usbPath = 'G:\stock_data'
startDate = datetime.today() - timedelta(days=70)
try:
data = pdr.get_data_yahoo(tickers, start=startDate, progress=False, interval="1d")
except Exception as e:
pass
for ticker in tickers:
dic = {'Open': data['Open'][ticker], 'High': data['High'][ticker], 'Low': data['Low'][ticker], 'Close': data['Close'][ticker]}
df = pd.DataFrame(dic)
df.to_csv(os.path.abspath(usbPath) + '/daily/{}.csv'.format(ticker))
Use os.path.join
使用
os.path.join
Ex: 例如:
import os
usbPath = r'G:\stock_data' #Note r in front of windows path
df.to_csv(os.path.join(usbPath, "daily", '{}.csv'.format(ticker)))
It is the most common mistake regarding this topic, it is a syntax error, if you change it to: 关于此主题,这是最常见的错误,如果将其更改为以下内容,则是语法错误:
usbPath= 'G:/stock_data'
it will work 它会工作
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.