Python中文
首页
教程
问答
标签
搜索
登录
注册
嵌套循环不会将字符串转换为datetime.date日期中途反对或退出
回答此问题可获得
20
贡献值,回答如果被采纳可获得
50
分。
<p>我有一个名为measures的字典列表,其中每个字典都包含一个日期键和一个字符串格式的日期值:</p> <p>度量值=</p> <pre><code>[{'date': '2010-01-01', 'prcp': 0.15142857142857144, 'tobs': 69.71428571428571}, {'date': '2010-01-02', 'prcp': 0.002857142857142857, 'tobs': 67.0}, {'date': '2010-01-03', 'prcp': 0.0, 'tobs': 74.0}, {'date': '2010-01-04', 'prcp': 0.0014285714285714286, 'tobs': 75.0}, {'date': '2010- 01-05', 'prcp': 0.005, 'tobs': 74.5}] </code></pre> <p>我想将日期字符串转换为datetime.date日期对象,因为我需要sqlalchemy的确切格式。我不想要一个日期时间。日期时间格式和我是肯定的当前格式是字符串格式。使用以下嵌套循环时:</p> <pre><code>for record in measures: for key, val in record.items(): record['date']=datetime.strptime(record['date'],'%Y-%m-%d').date() print(type(record['date'])) </code></pre> <p>我得到一个错误:TypeError:strTime()参数1必须是str,而不是datetime.date日期 但是在运行转换之后,注释转换代码并检查每个日期记录的类型,我看到有4个记录被转换为datetime.date日期但之后退出:</p> <pre><code>for record in measures: for key, val in record.items(): ##record['date']=datetime.strptime(record['date'],'%Y-%m-%d').date() print(type(record['date'])) Output: <class 'datetime.date'> <class 'datetime.date'> <class 'datetime.date'> <class 'str'> <class 'str'> </code></pre> <p>你知道我该怎么解决这个问题吗?我原来的字典里有两万条。你知道吗</p>
0 条评论
分类:
Python问答
请先
登录
后评论
默认排序
时间排序
1 个回答
匿名
1天前
擅长:python、mysql、java
<p>您的代码应为:</p> <pre><code>for record in measures: record['date']=datetime.strptime(record['date'],'%Y-%m-%d').date() print(type(record['date'])) </code></pre>
请先
登录
后评论
针对此问题:
更多的回答
关注
89
关注
收藏
1
收藏,
216
浏览
网友 提问于 2天前
相关Python问题
Python中两个字典的交集
9 回答
python中两个字符串上的异或操作数?
4 回答
Python中两个字符串中的类似句子
8 回答
Python中两个字符串之间的Hamming距离
7 回答
python中两个字符串之间的匹配模式
1 回答
python中两个字符串之间的按位或
4 回答
python中两个字符串之间的数据(字节)切片
4 回答
python中两个字符串之间的模式
6 回答
python中两个字符串作为子字符串的区别
3 回答
Python中两个字符串元组的比较
10 回答
Python中两个字符串列表中的公共字符串
6 回答
python中两个字符串的Anagram测试
8 回答
Python中两个字符串的正则匹配
4 回答
python中两个字符串的笛卡尔乘积
7 回答
Python中两个字符串相似性的比较
2 回答
python中两个字符串语义相似度的求法
1 回答
Python中两个字符置换成固定长度的字符串,每个字符的数目相等
7 回答
Python中两个对数方程之间的插值和平滑数据
8 回答
Python中两个对象之间的And/Or运算符
3 回答
python中两个嵌套字典中相似键的和值
2 回答