簡體   English   中英

在執行結束時編碼問題

[英]Encoding problems at the end of the execution

我的腳本存在編碼問題。 這是我的腳本:

def parse_airfields():
    html = urlopen('https://www.sia.aviation-civile.gouv.fr/aip/enligne/FRANCE/AIRAC-2015-09-17/html/eAIP/FR-AD-1.3-fr-FR.html').read()
    html = html.decode('utf-8')
    soup = BeautifulSoup(html, 'lxml')

    # A lot of work [....]

    return airfields


if __name__ == '__main__':
    airfields = parse_airfields()

    for airfield in airfields:
        for value in airfield.values():
            if isinstance(value, str):
                value.encode('utf-8')

    with open('airfields.json', 'w') as airfields_file:
        json.dump(airfields, airfields_file, indent=4, sort_keys=True)

我試過沒有encode()decode()但結果相同... JSON文件中的編碼問題: 我的json編碼問題

為什么呢 謝謝你的幫助!

str.encodebytes.decode不會修改該值; 您沒有分配value.encode('utf-8')的返回值,因此您實際上沒有進行任何更改。 當然,我不認為你真的想; json模塊僅適用於文本( str ),而不適用於二進制數據( bytes )。

問題是嚴格的JSON通常在其字符串中不包含非ASCII字符; 它使用轉義 ,例如 如果您告訴它,Python會直接輸出utf-8 ,只需在您的json.dump(...)調用的參數中添加ensure_ascii=False即可。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM