Python JSON抓取我将日期更新为'DueDate':'/ Date(1539205200000)/'。我没有使用日期格式

时间:2018-10-07 05:35:49

标签: python json python-3.x web-scraping scrapy

我的Json文件for o in odpg: try: duedate= o['DueDate'] print(duedate) except: # Handle error

代码:

/Date(1539205200000)/

我的输出为:

07/31/2018 

必需的输出:

{{1}}

2 个答案:

答案 0 :(得分:0)

您确定这是正确的号码吗?起初我以为是epoch,但是一年的结果太大了;但是,拿走最后三个0的作品,这使我想知道纪元是毫秒还是秒(野生波浪假设):

>>> import time
>>> time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime(1539205200000))
'50745-06-22 00:00:00'
>>> time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime(1539205200))
'2018-10-10 21:00:00'

>>> import datetime
>>> datetime.datetime.fromtimestamp(1539205200000)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: year is out of range
>>> datetime.datetime.fromtimestamp(1539205200)
datetime.datetime(2018, 10, 11, 7, 0)

要产生所需的输出,只需更改format string

>>> # Assuming constant format "/Date(…)/", this also removes the trailing zeroes..
>>> funky_timestamp = int(o["duedate"][6:-5])
>>> datetime.datetime.fromtimestamp(funky_timestamp).strftime("%d/%m/%Y")
'11/10/2018'

答案 1 :(得分:0)

import re
import datetime
json = {'DueDate': '/Date(1539205200000)/', 'Bydate': '/Date(-62135578800000)/', 'NeedsAppointment': False}
duedate = re.findall(r'[\d]+', json['DueDate'])
datetime.datetime.fromtimestamp(float(duedate[0][:10])).strftime("%d/%m/%Y")