我用随机数替换2010年所有出现在我的JSON文件1990年和2020年Python的类型错误:预期字符串或其他字符缓冲区对象
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())
之间我得到这个错误:
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
这里是这种情况发生的一个例子:
"myDate" : "2010_02",
是否有您的JSON文件的一些空行? – ettanany
@ettanany没有黑线!每一行至少有一个字符。 – cplus
@Mpondomise尝试我的解决方案 – eyllanesc