This is actually a string, as @Sushanth said:
from datetime import datetime
now = datetime.now()
temp=2.2341
humidity=2.123123
data = '[{{ "timestamp": "{0}", "temperature": "{1:0.1f}", "humidity": "{2:0.1f}" }}]'.format(now, temp, humidity)
print(data)
>>>[{ "timestamp": "2020-06-10 08:24:04.704800", "temperature": "2.2", "humidity": "2.1" }]
print(type(data))
>>><class 'str'>
Just it is created with format
which allows us to insert a specified value(s) inside the string's placeholder. And, to write this string into a file, you can try this:
text_file = open("sample.txt", "w")
n = text_file.write(data)
text_file.close()
And if you want to create a new file with this variable data
, you can checkout this link, and then open,write and close the file, as I showed you before.