Mac 自带词典 如何预览CSS

# -*- coding: utf-8 -*-
# https://gist.githubusercontent.com/josephg/5e134adf70760ee7e49d/raw/cf9787c363e40148d9fe58aff50d63b6fbee7341/0dedict.py
# Thanks to commenters for providing the base of this much nicer implementation!
# Save and run with $ python 0dedict.py
# You may need to hunt down the dictionary files yourself and change the awful path string below.
# This works for me on MacOS 10.14 Mohave

from struct import unpack
from zlib import decompress
from signal import signal, SIGPIPE, SIG_DFL
signal(SIGPIPE,SIG_DFL)
import re
filename='/System/Library/AssetsV2/./com_apple_MobileAsset_DictionaryServices_dictionaryOSX/b3b86a3a3219ad056e9d9bedf1c3f5572e248976.asset/AssetData/Simplified Chinese - English.dictionary/Contents/Resources/Body.data'

f = open(filename, 'rb')

def gen_entry():
    f.seek(0x40)
    limit = 0x40 + unpack('i', f.read(4))[0]
    f.seek(0x60)
    while f.tell()<limit:
        sz, = unpack('i', f.read(4))
        buf = decompress(f.read(sz)[8:])

        pos = 0
        while pos < len(buf):
                try:
                    chunksize, = unpack('i', buf[pos:pos+4])
                    pos += 4

                    entry = buf[pos:pos+chunksize]
                    title = re.search('d:title="(.*?)"', entry).group(1)
                    yield title, entry

                    pos += chunksize
                except:
                    print(entry)
                    pass

for word, definition in gen_entry():
    print(word+"\t"+ definition.replace('\n', '').replace('\r', ''))