[英]TypeError: argument of type 'datetime.date' is not iterable
I am having python script which contains fetching data from MySql DB and then parse that data 我有python脚本,其中包含从MySql DB中获取数据,然后解析该数据
below is the way I fetch data from MY SQL db 以下是我从MY SQL数据库中获取数据的方式
connection = MySQLdb.connect(host = "192.168.1.50", user = "root",
password = "root", db = "mydb", port = 32768)
cursor = connection.cursor()
cursor.execute("select * from licenses")
data = cursor.fetchall()
Below is small chunk of output 以下是一小部分输出
print(data)
((1,
'MS SQl',
'eaf5e1b212e0ad3c4a6172a9e9fd30145dd2d9bf070cc2568fdef384ab63254c',
datetime.date(2017, 2, 23),
Decimal('30000.00'),
'21022018',
2,
'',
1,
None,
datetime.datetime(2018, 2, 21, 9, 51, 22),
datetime.datetime(2018, 2, 21, 11, 26, 17),
None,
'Hudson Bay',
'hudson.bay@ifficeemailid.com',
None,
None,
datetime.date(2018, 2, 24),
'21022018',
datetime.date(2018, 2, 25),
1,
1,
0,
None),
(2,
'Palo Alto',
'eaf5e1b212e0',
datetime.date(2017, 2, 2),
Decimal('50000.00'),
'',
2,
'',
1,
None,
datetime.datetime(2018, 2, 21, 10, 5, 16),
datetime.datetime(2018, 2, 26, 12, 21, 51),
None,
'John Snow',
'john.snow@officeemailid.com',
None,
None,
datetime.date(2018, 2, 22),
'eaf5e1b212e0',
datetime.date(2018, 2, 22),
1,
1,
0,
None))
When I tried to get data from row 17 I can get 当我尝试从第17行获取数据时,我可以获取
for row in data:
print row[17]
2018-02-24
2018-02-22
However when I tried to add condition it wont work. 但是,当我尝试添加条件时,它将无法正常工作。
for row in data:
if '2018-02-24' in row[17]:
print('found')
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-50-a5803fbf5283> in <module>()
1 for row in data:
----> 2 if '2018-02-28' in row[17]:
3 print(yes)
4
TypeError: argument of type 'datetime.date' is not iterable
My Final Objective is getting date from code like = today = datetime.now().strftime("%Y-%m-%d")
On above code '2018-02-24' I have hardcoaded, in actual scenario I and getting that fro 'today' variable and check if that date is available in 'row 17' 我的最终目标是从= today = datetime.now().strftime("%Y-%m-%d")
这样的代码获取日期在上面的代码'2018-02-24'中,我在实际情况I和从“今天”变量中获取该变量,并检查“ 17行”中是否有该日期
but getting error, any way to solve this? 但是遇到错误,有什么办法解决吗?
You can use in
to search for the date in the entire column, not an individual object: 您可以使用in
搜索整个列中的日期,而不是单个对象:
today = datetime.date.today()
if today in (row[17] for row in data):
print('found')
There is no need to convert to string if you are comparing date objects. 如果要比较日期对象,则无需转换为字符串。 If you have datetime
instead of date
in the column, you need to do one extra transformation: 如果列中有datetime
而不是date
,则需要执行另一项转换:
today = datetime.date.today()
if today in (row[17].date() for row in data):
print('found')
The explicit generator can be replaced by either map(itemgetter(17), data)
or map(datetime.date, map(itemgetter(17), data))
. 显式生成器可以由map(itemgetter(17), data)
或map(datetime.date, map(itemgetter(17), data))
代替。 The latter is only necessary if you have a datetime
, which the error shows you don't: 仅当您有datetime
时才需要后者,该错误表明您没有:
from operator import itemgetter
today = datetime.date.today()
if today in map(itemgetter(17), data):
print('found')
Notice that none of these versions has an explicit loop. 请注意,这些版本都没有显式循环。 Since generators are iterable, in
will do a similar linear search to your original for
loop, but faster. 由于生成器是可迭代的,因此in
会执行与原始for
循环类似的线性搜索,但速度更快。 The only real difference, aside from speed, is that these examples will stop looking as soon as a match is found, but your loop does not. 除了速度之外,唯一真正的区别是,一旦找到匹配项,这些示例将立即停止查找,但您的循环没有找到。 You could achieve the same result by putting a break
statement in the if
. 通过在if
放置一个break
语句,可以达到相同的结果。
Try this, hope this helps. 试试这个,希望对您有所帮助。 Assuming row[17]
is date object. 假设row[17]
是日期对象。
import datetime
now = datetime.datetime.now()
tdy = now.strftime("%Y-%m-%d")
for row in data:
if tdy == row[17]:
print('found')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.