[英]Python: An integer is required. Where? Why?
I am having trouble with this brief code snippet: 我在这个简短的代码片段中遇到了麻烦:
for i, row in enumerate(rows):
row["last_check"] = last_check_ts
row_filtered = {
k: v.replace('\r', '') for k, v in row.iteritems() if v is not None
}
It gives me the following error: 它给了我以下错误:
k: v.replace('\r', '') for k, v in row.iteritems() if v is not None
TypeError: an integer is required
What am I doing wrong? 我究竟做错了什么? Exactly what is supposed to be an integer? 究竟是什么整数?
Edit: 编辑:
This is what rows
looks like printed on console: 这是在控制台上打印的rows
:
[{'COD_PER_FISICA': None, 'INCLUIDO_POR': 'FU', 'MODIFICADO_POR': None, 'ES_FISICA': 'N', 'COD_PERSONA': '37470', 'NOMBRE': 'BLABLA', 'COD_PER_JURIDICA': '37470', 'FECHA_MODIFICACION': None, 'FECHA_INCLUSION': datetime.datetime(2003, 6, 7, 18, 22, 13)},
{'COD_PER_FISICA': None, 'INCLUIDO_POR': 'FU', 'MODIFICADO_POR': 'FOO', 'ES_FISICA': 'N', 'COD_PERSONA': '37471', 'NOMBRE': 'blablabla', 'COD_PER_JURIDICA': '37471', 'FECHA_MODIFICACION': datetime.datetime(2003, 9, 2, 12, 14, 35), 'FECHA_INCLUSION': datetime.datetime(2003, 6, 7, 18, 22, 13)},
{'COD_PER_FISICA': None, 'INCLUIDO_POR': 'FU', 'MODIFICADO_POR': None, 'ES_FISICA': 'N', 'COD_PERSONA': '37472', 'NOMBRE': 'blablabala', 'COD_PER_JURIDICA': '37472', 'FECHA_MODIFICACION': None, 'FECHA_INCLUSION': datetime.datetime(2003, 6, 7, 18, 22, 13)},
{'COD_PER_FISICA': None, 'INCLUIDO_POR': 'FU', 'MODIFICADO_POR': None, 'ES_FISICA': 'N', 'COD_PERSONA': '37473', 'NOMBRE': 'blablabla', 'COD_PER_JURIDICA': '37473', 'FECHA_MODIFICACION': None, 'FECHA_INCLUSION': datetime.datetime(2003, 6, 7, 18, 22, 13)},
{'COD_PER_FISICA': None, 'INCLUIDO_POR': 'FU', 'MODIFICADO_POR': 'blablablaA', 'ES_FISICA': 'N', 'COD_PERSONA': '37474', 'NOMBRE': 'blablabla', 'COD_PER_JURIDICA': '37474', 'FECHA_MODIFICACION': datetime.datetime(2003, 9, 2, 12, 14, 19), 'FECHA_INCLUSION': datetime.datetime(2003, 6, 7, 18, 22, 13)}]
Move this line: 移动这一行:
row["last_check"] = last_check_ts
Below your dict comprehension. 低于您的字典理解力。 You can reproduce this as follows: 您可以按照以下方式重现此内容:
from datetime import datetime
rows = {'a': None, 'b': 'some_str\r', 'c': datetime.utcnow()}
{k: v.replace('\r', '') for k, v in rows.items() if v is not None}
You're adding a timestamp to your dict before looping over it, so one of the times you're calling v.replace('\\r', '')
the v
object is actually a datetime. 您需要在循环之前在字典中添加时间戳,因此调用v.replace('\\r', '')
v
对象的其中一次实际上是日期时间。 datetime
objects do indeed have a replace
method, but you're treating it like a string and therefore calling it with the wrong signature. datetime
对象确实有一个replace
方法,但是您将其视为字符串,因此使用错误的签名来调用它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.