但是 Messenger 內建的搜尋功能讀取太慢了
因此想要從備份紀錄下手
(註:備份檔是 2018-10 多匯出的,不清楚現在還有沒有這個 Bug)
找到對應群組的 message.json 後,發現裡面的訊息都會變成 \uxxxx 的格式
原本想說用 Python 幫我做轉換就好,畢竟 \uxxxx 是 JSON 標準可以使用的

圖片來源:https://json.org/
但是不管我怎麼試,轉出來的都是亂碼
我開始懷疑是 FB 匯出資料沒有處理好編碼
(不然就是我讀取的方式不對,但我實在找不到參數,檔案都用 'utf-8' 去讀取了)
例如原本的訊息是:
「特異點 • 黑洞」
FB 匯出是:
「\u00e7\u0089\u00b9\u00e7\u0095\u00b0\u00e9\u00bb\u009e \u00e2\u0080\u00a2 \u00e9\u00bb\u0091\u00e6\u00b4\u009e」
找線上 Unicode 轉換是:
「\u7279\u7570\u9ede \u2022 \u9ed1\u6d1e」
用線上 UTF-8 轉換是:
「\xe7\x89\xb9\xe7\x95\xb0\xe9\xbb\x9e\x20\xe2\x80\xa2\x20\xe9\xbb\x91\xe6\xb4\x9e」
有沒有發現奇怪的點?
後來找到這篇 Stackoverflow 有人找到處理方式
https://stackoverflow.com/questions/50799187/encoding-decoding-issue-with-facebook-json-messages-c-sharp-parsing
後來花一點時間寫個 python script 處理,如下
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
from pprint import pprint | |
import collections.abc | |
def transform(ob, func): | |
if isinstance(ob, collections.abc.Mapping): | |
result = {} | |
for k, v in ob.items(): | |
result[k] = transform(v, func) | |
return result | |
elif isinstance(ob, list): | |
result = [] | |
for v in ob: | |
result.append(transform(v, func)) | |
return result | |
elif isinstance(ob, str): | |
return func(ob) | |
return ob | |
with open('message.json', encoding='utf-8') as f: | |
data = json.load(f) | |
# https://stackoverflow.com/questions/50799187/encoding-decoding-issue-with-facebook-json-messages-c-sharp-parsing | |
data = transform(data, lambda s: s.encode('ISO-8859-1').decode('utf-8')) | |
# pprint(data) | |
with open('message-fixed.json', 'w', encoding='utf-8') as f: | |
json.dump(data, f, ensure_ascii=False, indent=4) | |
最後我終於找到以前分享的影片了
沒有留言:
張貼留言