【问题标题】:Using pandas to save to a csv file with date time appended to the filename使用 pandas 将日期时间附加到文件名的 csv 文件中
【发布时间】:2021-11-22 02:53:08
【问题描述】:

我正在运行代码,但出现此错误

OSError: [Errno 22] Invalid argument: 'filename 09-30-2021 16:45:17 PM.csv'

有没有办法解决这个问题?

currentDateTime = datetime.now().strftime("%m-%d-%Y %H:%M:%S %p")
df.to_csv(f"filename {currentDateTime}.csv", index = False)

【问题讨论】:

  • 您在 Windows 上吗?请查看受限字符。

标签: python-3.x pandas dataframe csv export-to-csv


【解决方案1】:

试试这个。因为,文件名中不能有冒号 (:)。

currentDateTime = datetime.now().strftime("%m-%d-%Y %H-%M-%S %p")
df.to_csv(f"filename {currentDateTime}.csv", index = False)

【讨论】:

    【解决方案2】:

    您的示例可以在 Linux 上运行,但由于字符受限,它不能在 Windows 上运行:< > : " / \ | ? *

    请将: 替换为其他内容。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-10-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-16
      相关资源
      最近更新 更多