Ricci流同胚于Chaos流吗 2022-01-24 23:21 采纳率: 60%
浏览 48
已结题

UnicodeEncodeError

先按原文贴出问题:
“UnicodeEncodeError: 'gbk' codec can't encode character '\U0001f917' in position 3664416: illegal multibyte sequence”
查遍了网上的方法,不得其解,可能是我碰到的这个问题的背景与网上解决方案的背景有所不同。这个问题我是在学习Python API用”plotly“可视化"Github"上的”repositories"的过程中碰到的。程序代码如下:

import requests
from plotly.graph_objs import Bar
from plotly import offline

url = 'https://api.github.com/search/repositories?q=language:python&sort=stars'
headers = {'Accept': 'application/vnd.github.v3+json'}
r = resquests.get(url, headers=headers)
print(f"Status code: {r.status_code}")

response_dict = r.json()
repo_dicts = response_dict['items']

repo_links, stars, labels = [], [], []
for repo_dict in repo_dicts:
    repo_name = repo_dict['name']
    repo_url = repo_dict['html_url']
    repo_link = f"<a href='{repo_url}'>{repo_name}</a>"
    repo_links.append(repo_link)

    stars.append(repo_dict['stargazers_count'])
     
    owner = repo_dict['owner']['login']
    description = repo_dict['description']
    label = f"{owner}<br />{description}"
    labels.append(label)

data = [{
    'type': 'bar',
    'x': repo_links,
    'y': stars,
    'hovertext': labels,
    'marker': {
           'color': 'rgb(60,100,150)',
           'line': {'width': 1.5, 'color': 'rgb(25,25,25)'},
    },
    'opacity': 0.6,
}]
my_layout = {
     'title': 'Most-Starred Python Projects on Github',
     'titlefont': {'size': 18, 'color': 'rgb(100,0,100)'},
     'xaxis': {
         'title': 'Repository',
         'titlefont': {'size': 14, 'color': 'rgb(100,0,100)'},
         'tickfont': {'size': 12, 'color': 'rgb(100,0,100)'},
     },
     'yaxis': {
         'title': 'Stars',
         'titlefont': {'size': 14, 'color': 'rgb(100,0,100)'},
         'tickfont': {'size': 12, 'color': 'rgb(100,0,100)'},
     },
}

fig = {'data': data, 'layout': my_layou}
offline.plot(fig, filename='python_repos_visual.html')

运行结果:
PS C:\Users......
Status code: 200
Traceback (most recent call last):
File "c:/Users......\python_repos_visual.py", line 60, in
offline.plot(fig, filename='python_repos_visual.html')
File "C:\Users......\Anaconda3\lib\site-packages\plotly\offline\offline.py", line 597, in plot
auto_open=auto_open,
File "C:\Users......\Anaconda3\lib\site-packages\plotly\io_html.py", line 542, in write_html
path.write_text(html_str)
File "C:\Users......\Anaconda3\lib\pathlib.py", line 1198, in write_text
return f.write(data)
UnicodeEncodeError: 'gbk' codec can't encode character '\U0001f917' in position 3664416: illegal multibyte sequence

直觉上判断,可能是几处HTML标记导致报错,于是对部分代码作修改:

# repo_links, stars, labels = [], [], [] 
# 修改为:
repo_names, stars, labels = [], [], []

# repo_url = repo_dict['html_url']
# repo_link = f"<a href='{repo_url}'>{repo_name}</a>"
# repo_links.append(repo_link)
# 修改为:
repo_names.append(repo_dict['name'])

# label = f"{owner}<br />{description}"
# 修改为:
label = f"{owner}"

#  'x': repo_links,
# 修改为:
‘x’: repo_names,

则程序可以正常运行:

img

修改后的代码失去了可单击的主页链接、以及鼠标放置条形图上显示完整更多信息的功能。

  • 写回答

3条回答 默认 最新

  • CSDN专家-HGJ 2022-01-25 01:38
    关注

    用plotly5.2.1运行代码没有问题,检查一下plotly版本,试试卸载重新安装一下。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 1月27日
  • 已采纳回答 1月26日
  • 创建了问题 1月24日

悬赏问题

  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表