转换为Sqlite数据库时选择编码

mgdq6dx1  于 2022-12-23  发布在  SQLite
关注(0)|答案(1)|浏览(162)

我正在将mbox文件转换为sqlite数据库。我不是来将数据库文件编码为utf-8的。
转换为db时,Python控制台显示以下消息:
第一个月
当我在DB Browser for SQlite上可视化数据时,特殊字符不会出现,而是显示符号。
我首先转换. text文件到Mbox文件与以下功能:

def makeMBox(fIn,fOut):
    if not os.path.exists(fIn):
        return False
    if os.path.exists(fOut):
        return False

    out = open(fOut,"w")

    lineNum = 0

    # detect encoding
    readsource =  open(fIn,'rt').__next__
    #fInCodec = tokenize.detect_encoding(readsource)[0]
    fInCodec = 'UTF-8'
    
    for line in open(fIn,'rt', encoding=fInCodec, errors="replace"):
        if line.find("From ") == 0:
            if lineNum != 0:
                out.write("\n")
            lineNum +=1
            line = line.replace(" at ", "@")
        out.write(line)
        
            
    out.close()
    return True

然后,我转换为sqlite db:

for k in dates:

    db = sqlite_utils.Database("Courriels_Sqlite/Echanges_Discussion.db")
    mbox = mailbox.mbox("Courriels_MBox/"+k+".mbox")

    def to_insert():
        for message in mbox.values():
            Dionyversite = dict(message.items())
            Dionyversite["payload"] = message.get_payload()
            yield Dionyversite

    try:
        db["Dionyversite"].upsert_all(to_insert(), alter = True, pk = "Message-ID")
    except sql.InterfaceError as e:
        print(e)

谢谢你的帮助。

r7xajy2e

r7xajy2e1#

我找到了解决办法:

def to_insert():
        for message in mbox.values():
            Dionyversite = dict(message.items())
            Dionyversite["payload"] = message.get_payload(decode = True)
            yield Dionyversite
``

相关问题