Appengine Python Bulk Export错误

时间:2010-05-12 14:13:36

标签: python google-app-engine

这似乎运行约42,200条记录然后失败:

import datetime
import time

from google.appengine.ext import db
from google.appengine.tools import bulkloader
from google.appengine.api import datastore_types

class SearchRec(db.Model):
  WebSite = db.StringProperty()
  WebPage = db.StringProperty()
  DateStamp = db.DateTimeProperty(auto_now_add=True)
  IP = db.StringProperty()
  UserAgent = db.StringProperty()


class TrackerExporter(bulkloader.Exporter):
    def __init__(self):
        bulkloader.Exporter.__init__(self, 'SearchRec',
                                    [('WebSite', str, None),
                                    ('WebPage', str, None),
                                    ('DateStamp', lambda x: str(datetime.datetime.strptime(x, '%d/%m/%Y').date()), None),
                                    ('IP', str, None)
                                    ])

exporters = [TrackerExporter]

if __name__ == '__main__':
    bulkload.main(TrackerExporter)

错误:

File "tracker-export.py", line 89, in <lambda>
        ('DateStamp', lambda x: str(datetime.datetime.strptime(x, '%d/%m/%Y').date()
    ), None),

TypeError: strptime() argument 1 must be string, not datetime.datetime

1 个答案:

答案 0 :(得分:1)

我不确定为什么会发生这种情况。我对App Engine的Bulk Exporting工具不太熟悉,但听起来DateStamp字段作为字符串(这是转换器所期望的)为批量导出器提供给前42200个记录,然后,由于某种原因,它被作为一个真正的datetime.datetime对象给出。

无论如何,这是针对这种特殊症状的治疗方法:

lambda x: str((x if isinstance(x, datetime.datetime) else datetime.datetime.strptime(x, '%d/%m/%Y')).date())

如果给出字符串或datetime.datetime对象,那应该是正确的。这可能是一种情况,你应该给它一个专门的功能,而不是像这样折磨lambda