2
我想打印類似下面使用Python的字符串:逃逸STR格式括號
{"_id":ObjectId("5a43ae09e2bae06ddd400dfc")}
起初我還以爲會很容易,但事實並非如此。
這是我第一次嘗試:
'{"_id":ObjectId("{}")}'.format('5a43ae09e2bae06ddd400dfc')
但我得到了以下錯誤
Traceback (most recent call last): File "", line 1, in KeyError: '"_id"'
然後我嘗試:
'\{"_id":ObjectId("{}")\}'.format('5a43ae09e2bae06ddd400dfc')
而且我得到這個錯誤
我設法使其與下面的代碼工作:
'{"_id":ObjectId("%s")}' %('5a43ae09e2bae06ddd400dfc')
'{"_id":ObjectId("5a43ae09e2bae06ddd400dfc")}'
有什麼不對海峽格式?
謝謝!但我仍然感到有點奇怪,爲什麼不像'\'那樣用慣用方括號括住 – zhuguowei