从 google-app-engine 加载数据时如何格式化日期?
我使用 remote_api
从 Google App Engine 加载数据。
appcfg.py download_data --config_file=helloworld/GreetingLoad.py --filename=a.csv --kind=Greeting helloworld
设置是:
class AlbumExporter(bulkloader.Exporter):
def __init__(self):
bulkloader.Exporter.__init__(self, 'Greeting',
[('author', str, None),
('content', str, None),
('date', str, None),
])
exporters = [AlbumExporter]
我下载a.csv
。日期不可读。
如何获得完整的日期?
我改变了这一点:
class AlbumExporter(bulkloader.Exporter):
def __init__(self):
bulkloader.Exporter.__init__(self, 'Greeting',
[('author', str, None),
('content', str, None),
('date', lambda x: datetime.datetime.strptime(x, '%m/%d/%Y').date(), None),
])
exporters = [AlbumExporter]
但是,我仍然收到错误。
I use remote_api
to load data from Google App Engine.
appcfg.py download_data --config_file=helloworld/GreetingLoad.py --filename=a.csv --kind=Greeting helloworld
The setting is:
class AlbumExporter(bulkloader.Exporter):
def __init__(self):
bulkloader.Exporter.__init__(self, 'Greeting',
[('author', str, None),
('content', str, None),
('date', str, None),
])
exporters = [AlbumExporter]
And I download a.csv
. The date is not readable.
How to get the full date?
I changed this:
class AlbumExporter(bulkloader.Exporter):
def __init__(self):
bulkloader.Exporter.__init__(self, 'Greeting',
[('author', str, None),
('content', str, None),
('date', lambda x: datetime.datetime.strptime(x, '%m/%d/%Y').date(), None),
])
exporters = [AlbumExporter]
However, I am still getting an error.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来当您说时,您在第三列的空格处被截断
(另一种尝试显然是错误的,因为您得到的是
datetime
并且不能strptime那个!-)。如果您希望日期为字符串,请尝试:
或者在第二次尝试中将
strptime
更改为strftime
。助记:strftime
中的f
代表 format:获取日期时间并将其格式化为字符串;strptime
中的p
代表 parse:获取一个字符串并从中生成日期时间。它们是旧名称,来自 ANSI C 标准库(甚至更早的影响)...Looks like you're getting a truncation at the space in the third column when you say
(the other attempt is clearly wrong because you're getting a
datetime
and you can'tstrptime
that!-). If you want the date as a string, try:or, change
strptime
tostrftime
in your second attempt. Mnemonic: thef
instrftime
stands for format: take a datetime and format it to a string; thep
instrptime
stands for parse: take a string and make a datetime out of it. They're old names, coming from the standard library for ANSI C (and even-earlier influences on it)...