I am replacing all the occurrences of 2010 in my JSON file with a random number between 1990 and 2020.
import fileinput
from random import randint
f = fileinput.FileInput('data.json', inplace=True, backup='.bak')
for line in f:
print(line.replace('2010', randint(1990, 2020)).rstrip())
I get this error:
Traceback (most recent call last): File "replace.py", line 5, in
print(line.replace('2010', randint(1990, 2020)).rstrip()) TypeError: expected a string or other character buffer object
And here is one example of such an occurrence:
"myDate" : "2010_02",
Best Answer
The new value must be string, but you are passing a value of type int.
change :
to: