json - Python scriptline to get date&time -
as part of xml-file have following format of timestamp-string:
<time_stamp>20170120 14:38</time_stamp> in python-script generate xml-file (as equivalent json-file), following 2 instructions & print info date&time:
now = datetime.datetime.now() print 'present date & time = ', the result of print-line like:
present date & time = 2017-04-07 18:19:35.538690 as explained below question, first scriptline now=datetime.datetime.now() apparently not suitable well-formatted info desired xml-line , it's equivalent json-line.
question: better replacement scriptline?
to put timestamp-info json-dictionary , print result, have following 2 script-lines:
dds238_dict['time_stamp'] = print(dds238_dict) for timestamp printline has output like:
'time_stamp': datetime.datetime(2017, 4, 7, 18, 19, 35, 538690) generation of json-file , xml-file following 9 lines in python-script:
# make json-file open('dds238_dict.json', 'w') outfile: json.dump(dds238_dict, outfile) # convert dictionary xml-file & print dds238_xml = dicttoxml.dicttoxml(dds238_dict, attr_type=false) print(dds238_xml) xml_output = open("dds238_status.xml",'w') xml_output.write(dds238_xml) xml_output.close() for generation of json-file following error-report:
typeerror: datetime.datetime(2017, 4, 7, 18, 19, 35, 538690) not json serializable from generation of xml-file following result timestamp-string:
<time_stamp>2017-04-07t18:31:14.664248</time_stamp> answer:
helped hints link in stackoverflow [ how print date in regular format in python? ] guided working solution:
=> add 1 scriptline correct formatting of date&time, before putting timestamp-info json-dictionary
dattim = datetime.datetime.now().strftime('%y%m%d %h:%m') dds238_dict['time_stamp'] = dattim print(dds238_dict)
Comments
Post a Comment