gpt4 book ai didi

azure - IPython Notebook 中的奇怪符号

转载 作者:行者123 更新时间:2023-12-03 03:11:55 24 4
gpt4 key购买 nike

我在 IPython 笔记本中使用西里尔字母符号。当我在 ML studio 工作时它工作得很好。

但是当我下载笔记本并打开它们时(例如 http://try.jupyter.org ),我看到了奇怪的字符。

错误笔记本(在 Azure ML Studio 上创建):

{"nbformat_minor": 0, "cells": [{"source": "\u00d1\u0082\u00d0\u00b5\u00d1\u0081\u00d1\u0082", "cell_type": "markdown", "metadata": {"collapsed": true}}], "nbformat": 4, "metadata": {"kernelspec": {"display_name": "Python 2", "name": "python2", "language": "python"}, "language_info": {"mimetype": "text/x-python", "nbconvert_exporter": "python", "version": "2.7.11", "name": "python", "file_extension": ".py", "pygments_lexer": "ipython2", "codemirror_mode": {"version": 2, "name": "ipython"}}}}

$ file bad.ipynb
bad.ipynb: ASCII text, with very long lines, with no line terminators

“良好”版本,创建于 http://try.jupyter.org :

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"тест"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.10"
}
},
"nbformat": 4,
"nbformat_minor": 0
}

$ file good.ipynb
good.ipynb: UTF-8 Unicode text

最佳答案

我做了一些实验,发现你的json被编码成utf-8。对于您的情况,获取真实内容很简单。请参阅下面的代码:

Python 3.x

a = '{"nbformat_minor": 0, "cells": [{"source": "\u00d1\u0082\u00d0\u00b5\u00d1\u0081\u00d1\u0082", "cell_type": "markdown", "metadata": {"collapsed": true}}], "nbformat": 4, "metadata": {"kernelspec": {"display_name": "Python 2", "name": "python2", "language": "python"}, "language_info": {"mimetype": "text/x-python", "nbconvert_exporter": "python", "version": "2.7.11", "name": "python", "file_extension": ".py", "pygments_lexer": "ipython2", "codemirror_mode": {"version": 2, "name": "ipython"}}}}'

result = a.encode('latin-1').decode("utf-8")

Python 2.x

a = '{"nbformat_minor": 0, "cells": [{"source": "\u00d1\u0082\u00d0\u00b5\u00d1\u0081\u00d1\u0082", "cell_type": "markdown", "metadata": {"collapsed": true}}], "nbformat": 4, "metadata": {"kernelspec": {"display_name": "Python 2", "name": "python2", "language": "python"}, "language_info": {"mimetype": "text/x-python", "nbconvert_exporter": "python", "version": "2.7.11", "name": "python", "file_extension": ".py", "pygments_lexer": "ipython2", "codemirror_mode": {"version": 2, "name": "ipython"}}}}'

result = a.decode('unicode-escape').encode("latin-1")

这段代码可能不适用于其他一些情况,因为“latin-1”没有涵盖所有 0-255 个字符。因此,我仍在寻找一种更好的编码来处理此类事情。

关于azure - IPython Notebook 中的奇怪符号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36194987/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com