【发布时间】:2019-12-04 10:59:13
【问题描述】:
我有一个从 pandas 数据框创建的 csv。
但是一旦我附加它,它就会抛出:OSError: [Errno 95] Operation not supported
for single_date in [d for d in (start_date + timedelta(n) for n in range(day_count)) if d <= end_date]:
currentDate = datetime.strftime(single_date,"%Y-%m-%d")
#Send request for one day to the API and store it in a daily csv file
response = requests.get(endpoint+f"?startDate={currentDate}&endDate={currentDate}",headers=headers)
rawData = pd.read_csv(io.StringIO(response.content.decode('utf-8')))
outFileName = 'test1.csv'
outdir = '/dbfs/mnt/project/test2/'
if not os.path.exists(outdir):
os.mkdir(outdir)
fullname = os.path.join(outdir, outFileName)
pdf = pd.DataFrame(rawData)
if not os.path.isfile(fullname):
pdf.to_csv(fullname, header=True, index=False)
else: # else it exists so append without writing the header
with open(fullname, 'a') as f: #This part gives error... If i write 'w' as mode, its overwriting and working fine.
pdf.to_csv(f, header=False, index=False, mode='a')
【问题讨论】:
-
不确定这是否有帮助,但有几次我读到 pandas 的文件方法不应该获取句柄,而是文件名/路径。此外,有了这个你就不需要上下文管理器,因为函数本身负责关闭(如果你给它一个路径)。
-
@OlegO 我已经尝试过不使用上下文管理器。但它同样的错误。
-
这行我没看懂:pdf.to_csv(f, header=True, index=False) what's f in here?
-
@OlegO 对不起,我想我写错了。刚刚更正了第一个 pdf.to_csv
标签: python-3.x pandas azure-databricks