【问题标题】:Weird symbols in IPython NotebookIPython Notebook 中的奇怪符号
【发布时间】:2016-03-24 07:19:10
【问题描述】:

我在 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

【问题讨论】:

  • 问题在于编码。你看“тест”不是ascii。因此,Json 解析器将它们转换为 Unicode。您需要知道编码才能获得真实的内容。
  • 是的,我明白为什么会发生这种情况,但我不明白如何解决这个问题。

标签: azure encoding jupyter-notebook azure-machine-learning-studio


【解决方案1】:

我做了一些实验,发现你的 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 个字符。因此,我仍在为这类事情寻找更好的编码。

【讨论】:

  • 谢谢,看来此代码仅适用于 Python 3 内核。但至少我现在可以恢复我的笔记本了。
  • 涉及编码时,会发生奇怪的事情。我不知道为什么 python 2.x 代码会这样。我刚试了一下。
猜你喜欢
  • 2016-02-22
  • 1970-01-01
  • 1970-01-01
  • 2013-08-16
  • 1970-01-01
  • 1970-01-01
  • 2016-04-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多