【问题标题】:UnicodeDecodeError: 'charmap' codec can't decode byte 0x81 in position 49: character maps to <undefined>UnicodeDecodeError:“charmap”编解码器无法解码位置 49 中的字节 0x81:字符映射到 <undefined>
【发布时间】:2019-10-20 04:47:57
【问题描述】:

我正在创建一个特定的函数,它可以为我提供有关 ISS(国际空间站)和给定位置(以十进制坐标表示)的信息,这些信息可能会因输入而异。但是当我使用这个时:

print(ubicacion.raw['address']['country'],",",ubicacion.raw['address']['city'])

它有效,但对于某些国家,例如当我尝试使用堪培拉坐标时,它会显示以下信息:

Corinna Street, Phillip, 沃登谷区, 澳大利亚首都直辖区, 2606, 澳大利亚

由于它不提供城市,所以当我使用密钥 "city" 时,我显然会收到错误,因为它不存在于该列表中。

所以我想到的一个解决方案是,至少我总是会得到国家,也许我可以使用另一个函数,基于国家,我可以得到首都,这是我需要的它存在,我使用了"CountryInfo" (from countryinfo import CountryInfo)。问题是,当我尝试使用它时,出现以下错误:

UnicodeDecodeError:“charmap”编解码器无法解码位置 49 中的字节 0x81:字符映射到

我已经搜索过类似的问题,很多人建议指定编码,但它似乎不适用于geopy。因为我试过这个:

countryinfo=CountryInfo(country,encoding="utf8") 

我得到了这个错误:

TypeError: init() 得到了一个意外的关键字参数“编码”

【问题讨论】:

    标签: python python-3.x geolocation nominatim


    【解决方案1】:

    引用自:https://python-forum.io/Thread-Countryinfo-package-charmap-error

    来自@snippsat 的回答 2018 年 9 月 14 日上午 11:37

    在 ..Lib\site-packages\countryinfo 文件夹中打开 countryinfo.py。 将行更改为:

    country_info = json.load(open(file_path, encoding='utf-8'))
    

    它对我有用。

    【讨论】:

    • 有效。那么为什么 Python 一开始不识别 utf-8 呢?
    【解决方案2】:
    from countryinfo import CountryInfo
    
    country = CountryInfo('Singapore')
    country.capital()
    # returns string
    'Singapore'
    

    country.capital() 将以字符串格式为您获取首都

    【讨论】:

    • 好吧,这对我不起作用,我从标题中得到了错误。
    • UnicodeDecodeError: 'charmap' codec can't decode byte 0x81 in position 49: character maps to
    • 在评论搜索“CountryInfo Python”时出现在 Google 中的第一件事之前,请至少阅读我的帖子...
    猜你喜欢
    • 2017-02-06
    • 2021-05-06
    • 2021-04-11
    • 2020-02-02
    • 1970-01-01
    • 1970-01-01
    • 2019-06-01
    • 1970-01-01
    相关资源
    最近更新 更多