You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python调用format函数报'NoneType'对象无format属性错误

报错根本原因

.format()方法的调用顺序完全写反了。
pandas中DataFrame.to_csv()方法执行写入操作后没有返回值,默认返回None。你的现有代码执行逻辑是:

  1. 先把带{}占位符的原始路径字符串传给to_csv(),执行写入操作
  2. 拿到to_csv()的返回值None
  3. 尝试对None调用.format("test")方法

None类型本身没有format属性,因此直接抛出对应AttributeError异常。

修复方法

先对路径字符串完成格式化,再将最终生成的有效路径作为参数传入to_csv()即可。
修正后的代码:

df_data.to_csv(r"/dbfs/mnt/pdf-recognized/FinalInvestmentReport/{}.txt".format("test"), index=False)

如果使用Python 3.6及以上版本,也可以用f-string写法简化路径拼接,可读性更高:

report_name = "test"
df_data.to_csv(rf"/dbfs/mnt/pdf-recognized/FinalInvestmentReport/{report_name}.txt", index=False)

内容的提问来源于stack exchange,提问作者Patterson

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.01 17:01:27