1

I've been searching a solution for this problem all over the web with no luck. I am trying to concatenate a string with a datetime object to form a .json format file however for some reason there is an error while doing so. This is the code:

data = '{"gpio":"00000000","timestamp":'+str(int(time()))+',"formatted_time":"'+ **str(datetime.datetime.now().strftime("%A %b %d %X"))**+'""","time_zone":"'+str(read_tz_file())+'","firmware":"0.0"}

The even weirder scenario is that when adding any key after the method call it seems to be ok.

5
  • 1
    What is the error? You didn't mention that. Commented Sep 15, 2015 at 16:51
  • Was the error useful? Commented Sep 15, 2015 at 16:51
  • 2
    Have you tried using Python's json library? Commented Sep 15, 2015 at 16:52
  • FWIW, that code's really painful to read, all on one line like that. But anyway, what do you expect **str(datetime.datetime.now().strftime("%A %b %d %X"))** to do? That's not legal Python syntax. Commented Sep 15, 2015 at 16:53
  • I think he was trying to bold text inside the code Commented Sep 15, 2015 at 16:55

3 Answers 3

3

If you're writing/reading json, use the json library:

import json
print json.dumps(
    dict(gpio='00000000',
         timestamp=time(),
         formatted_time=datetime.datetime.now().strftime("%A %b %d %X"),
         time_zone=read_tz_file(),
         firmware='0.0'))
Sign up to request clarification or add additional context in comments.

1 Comment

For some reason still dosent work when writing to a .json file.
1

for starters, it might help to put the code in a readable form, preferrably multi-line (e.g. one json element per line).

this makes it easy to spot quoting errors.

data = ('{' +
    '"gpio":"00000000",'+
    '"timestamp":'+str(int(time()))+','+
    '"formatted_time":"'+ str(datetime.datetime.now().strftime("%A %b %d %X")) +','+
    '""",'+
    '"time_zone":"'+str(read_tz_file())+'",'+
    '"firmware":"0.0"'+
    '}')

then try to debug the errors one by one.

e.g. str(int(time())) bails out at me, with a:

Traceback (most recent call last): File "", line 1, in TypeError: 'module' object is not callable

that's because time is a module not a function, the proper function would be time.time():

data = ('' +
    '{"gpio":"00000000",'+
    '"timestamp":'+str(int(time.time()))+','+
    '"formatted_time":"'+ str(datetime.datetime.now().strftime("%A %b %d %X")) +','+
    '""",'+
    '"time_zone":"'+str(read_tz_file())+'",'+
    '"firmware":"0.0"'+
    '}')

this gives me a valid string (after providing a dummy implementation of read_tz_file(), but it is invalid JSON (what's that """ supposed to do`)

a better way would be to construct a dictionary first, and convert that do json:

import json
d={
  "gpio": 0,
  "timestamp": int(time.time()),
  "formatted_time": (datetime.datetime.now().strftime("%A %b %d %X"),
  "time-zone": read_tz_file(),
  "firmware": "0.0"
}
s=json.dumps()
print(s)

2 Comments

Thanks for your solution. Sorry for the syntax errors in the code, obviously they were not intentional. The error is not from the syntax, however the .json file does not get updated after writing the concatenation to the file. I tried your approach however this still does not solve my issue.
which file? you never mentioned anything about a file. your question is about concatenating a string in python, not about updating a file. (so please don't expect people to solve unmentioned problems).
1

Use json module, to generate json text. Use the same Unix time for timestamp and formatted_time:

import json
import time

ts = int(time.time())
json_text = json.dumps(dict(
    gpio="00000000",
    timestamp=ts,
    formatted_time=time.strftime("%A %b %d %X", time.localtime(ts)),
    time_zone=read_tz_file(),
    firmware="0.0"))

Note: in general, time.localtime(ts) may provide more info than datetime.now() e.g. in Python 2:

>>> import time
>>> from datetime import datetime
>>> ts = time.time()
>>> time.strftime('%Z%z')
'CEST+0200'
>>> time.strftime('%Z%z', time.localtime(ts))
'CEST+0000'
>>> datetime.now().strftime('%Z%z')
''
>>> datetime.fromtimestamp(ts).strftime('%Z%z')                                                          
''

Notice: only time.strftime('%Z%z') provides complete info for the local timezone on my machine, see python time.strftime %z is always zero instead of timezone offset.

On Python 3, datetime.now() too does not provide info about the local timezone:

>>> import time
>>> from datetime import datetime
>>> ts = time.time()
>>> time.strftime('%Z%z')
'CEST+0200'
>>> time.strftime('%Z%z', time.localtime(ts))
'CEST+0200'
>>> datetime.now().strftime('%Z%z')
''
>>> datetime.fromtimestamp(ts).strftime('%Z%z')
''

You could workaround it:

>>> from datetime import timezone
>>> datetime.now(timezone.utc).astimezone().strftime('%Z%z')
'CEST+0200'
>>> datetime.fromtimestamp(ts, timezone.utc).astimezone().strftime('%Z%z')
'CEST+0200'

If you want to work with datetime in Python 3; your code could look like:

#!/usr/bin/env python3
import json
from datetime import datetime, timedelta, timezone

epoch = datetime(1970, 1, 1, tzinfo=timezone.utc)
local_time = datetime.now(timezone.utc).astimezone()
json_text = json.dumps(dict(
    gpio="00000000",
    timestamp=(local_time - epoch) // timedelta(seconds=1),
    formatted_time=local_time.strftime("%A %b %d %X"),
    time_zone=read_tz_file(),
    firmware="0.0"))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.