1. 使用jupyter報錯: Type:error
原代碼如下:
for i in item_info.find().limit(10):
frags = i['pub_date'].split('-') # 分割會產生列表,這樣就會生成一個切成3個元素的列表,如果不分割直接打印則對應字符串*
if len(frags) == 1:
date = frags[0] # 原樣輸出
else:
date = '{}.{}.{}'.format(frags[0],frags[1],frags[2])
print(date)
item_info.update({'_id',i['_id']}, { '$set':{'pub_date':date} }) # 更新到數據庫,注意記憶
上述代碼運行報錯,報錯如下:
/usr/local/lib/python3.6/site-packages/ipykernel_launcher.py:9: DeprecationWarning: update is deprecated. Use replace_one, update_one or update_many instead.
if __name__ == '__main__':
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-183-59d59858c2fb> in <module>()
7
8 print(date)
----> 9 item_info.update({'_id',i['_id']}, { '$set':{'pub_date':date} })
...............
TypeError: spec must be an instance of dict, bson.son.SON, or other type that inherits from collections.Mapping
解法
通過報錯內容我們可以大概猜到了,update使用錯誤,應該是item_info.update_one()
\r\n 出現在字符串內,表示換一行,重復2遍就是換2行,類推
In [5]: print('abd\r\n\r\n\r\n')
abd # 注意數空行數就明白了
In [6]: