开发者

How to format date when I load data from google-app-engine?

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.


Looks like you're getting a truncation at the space in the third column when you say

('date', str, None),

(the other attempt is clearly wrong because you're getting a datetime and you can't strptime that!-). If you want the date as a string, try:

('date', lambda dt: str(dt.date()), None),

or, change strptime to strftime in your second attempt. Mnemonic: the f in strftime stands for format: take a datetime and format it to a string; the p in strptime 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)...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜