python - TypeError:强制转换为 Unicode:需要找到字符串或缓冲区日期时间日期时间
问题描述
这是我将输出保存到csv
文件中的脚本:
import os, csv, datefinder, datetime
os.chdir('C:\Users\dul\Desktop\Article')
with open("test2.txt", 'r') as file1:
text1 = file1.read()
matches = list(datefinder.find_dates(text1))
if len(matches) > 0:
date = matches[1]
print date
else:
print 'No dates found'
csv = open(date, "w")
columnTitleRow = "date, time\n"
csv.write(columnTitleRow)
当我运行此脚本时,我收到以下错误消息:
[回溯(最近一次调用最后一次):文件“C:\Users\dul\Desktop\Article\ap.py”,第 18 行,在 csv = open(date, "w") 类型错误:强制转换为 Unicode:需要字符串或缓冲区,找到日期时间.日期时间]
解决方案
您不能将datetime
实例传递给open()
; 该函数需要一个字符串来指示它应该打开的文件的文件名。
您确定需要打开一个具有日期名称的文件吗?如果确实如此,那么您至少需要将datetime
对象转换为 a str
:
csv = open(str(date), "w")
但我怀疑这真的是你需要的。
推荐阅读
- api - 如何从 newman 的集合中运行单个请求
- dynamic - 从 PL/SQL 包中的动态字符串(立即执行)创建数据库视图 - 有问题吗?
- python - 从Python中的字符串列表中获取十进制数/整数之前的所有值
- scala - 在 Spark 中创建数据集时遇到错误
- corda - 使用 MockNetwork 时如何将不同的 CorDapp 配置传递给不同的节点
- javascript - 如何从 MongoDB 中删除已发布的问题
- tensorflow - 如何解决,没有名为'tf'的模块?
- php - 连接数据库失败
- hyperledger-fabric-sdk-js - 使用fabric 2.0中的fabric-sdk-node提交Chaincode时没有启动Chaincode容器
- php - php://输入直接到浏览器