简体   繁体   English

在 vscode 中打开 ipynb 文件时,意外令牌 # in JSON at position 0

[英]Unexpected token # in JSON at position 0 when opening ipynb file in vscode

I have a ipynb file (a jupyter notebook) which I am opening in vscode with python extension.我有一个 ipynb 文件(一个 jupyter 笔记本),我在 vscode 中打开它,扩展名为 python。 I receive the error in the title我收到标题中的错误

Unexpected token # in JSON at position 0

which I dont understand at all, since the file is supposed to be interpreted as a python file.我根本不明白,因为该文件应该被解释为 python 文件。

I can change the extension to.py and its opened fine by vscode, but I dont have the decorators to run/debug cells like define here ( https://code.visualstudio.com/docs/python/jupyter-support-py ).我可以将扩展名更改为 .py 并通过 vscode 正常打开它,但我没有像在此处定义的那样运行/调试单元格的装饰器 ( https://code.visualstudio.com/docs/python/jupyter-support-py ) .

I know the file is correct because I have use it in another vscode installation in another computer and works fine.我知道该文件是正确的,因为我已经在另一台计算机的另一个 vscode 安装中使用它并且工作正常。

I have no idea what might be misconfigured in my environment... Any tops would be really helpful.我不知道在我的环境中可能配置错误的是什么......任何顶部都会非常有帮助。

Here is the actual python code I have that its producing the mentioned error my actual environment.这是我拥有的实际 python 代码,它会在我的实际环境中产生上述错误。

issue.ipynb问题.ipynb

# %%
import world as w
import world_eg as weg
import world_case1 as wc1
import simulator_static as simulation
import numpy as np
from scipy.optimize import minimize
import matplotlib.pyplot as plt```

From the error, I understand that is parsing the file as a JSON file and the first line, which contains the #, fails. 

This happens when you make a request to the server and parse the response as JSON, but it's not JSON.当您向服务器发出请求并将响应解析为 JSON,但它不是 JSON 时,就会发生这种情况。 JSON should start with a valid JSON value – an object, array, string, number, or false/true/null. JSON 应以有效的 JSON 值开头 - object、数组、字符串、数字或 false/true/null。 The root cause is that the server returned HTML or some other non-JSON string.根本原因是服务器返回了 HTML 或其他一些非 JSON 字符串。

I've tried your code in my project and nothing wrong.我已经在我的项目中尝试了你的代码,没有错。 everything looks fine.一切看起来都很好。 Check the Jupyter Server network, try to restart vscode and recreate a new juypter file, and see if the problem goes away.检查 Jupyter Server 网络,尝试重启 vscode 并重新创建一个新的 juypter 文件,看看问题是否消失。

在此处输入图像描述

[edit] [编辑]

like the above screenshot shows, type # %% will add a new cell.如上面的屏幕截图所示,键入# %%将添加一个新单元格。 Equally, when you open a.ipynb file, if python extension distinguishes the # %% , button run cell | debug cell同样,当您打开 a.ipynb 文件时,如果 python 扩展名区分# %% ,则按钮run cell | debug cell run cell | debug cell will be displayed automatically for you to do further test. run cell | debug cell将自动显示,供您进行进一步测试。

在此处输入图像描述

you can copy your code without # %% to a new created blank juypter file, then click the button export as and select Python Script to got button Run Cell | Debug Cell您可以将不带# %%的代码复制到新创建的空白 juypter 文件中,然后单击按钮export as select Python Script以获取按钮Run Cell | Debug Cell Run Cell | Debug Cell . Run Cell | Debug Cell

OR reinstall python extension and try again.或者重新安装 python 扩展并重试。

I had a similar problem and when I opened the notebook with an editor I saw I had merge markings that git had put into the file.我遇到了类似的问题,当我用编辑器打开笔记本时,我看到 git 已放入文件中的合并标记。 eg例如

<<<<<<< HEAD
...
=======
...
>>>>>>> ...

Cleaning up these, allowed jupyter to parse the file and run the notebook.清理这些,允许 jupyter 解析文件并运行笔记本。

I had the same issue and for me that problem was solved simply by deleting the underscore (_) from the file name.我遇到了同样的问题,对我来说,只需从文件名中删除下划线 (_) 即可解决该问题。 I don't know why but it works.我不知道为什么,但它有效。

.ipynb files aren't actually Python source code files - they're encoded as JSON files. .ipynb文件实际上并不是 Python 源代码文件 - 它们被编码为 JSON 文件。 If you create a new notebook, then rename the file extension or open it in some text editor, you'll see the structure of the underlying JSON file.如果您创建一个新笔记本,然后重命名文件扩展名或在某些文本编辑器中打开它,您将看到底层 JSON 文件的结构。

When VS Code tries to interpret your file, it's trying to parse Python source as a JSON object, which will obviously fail, and lead to the otherwise cryptic error of an unexpected token.当 VS Code 尝试解释您的文件时,它会尝试将 Python 源解析为 JSON object,这显然会失败,并导致意外的加密错误。

In other words, it's not possible to convert a Python script into a notebook just by changing the file extension.换句话说,仅通过更改文件扩展名就无法将 Python 脚本转换为笔记本。 Manually copying & pasting the code around will work, or you could try googling for some tool, eg https://github.com/remykarem/python2jupyter手动复制和粘贴代码将起作用,或者您可以尝试使用谷歌搜索某些工具,例如https://github.com/remykarem/python2jupyter

I had a similar issue when creating a new file in VS Code which I saved as.ipynb.在我保存为 .ipynb 的 VS Code 中创建新文件时,我遇到了类似的问题。 After closing the file, I was not able to reopen and I received the same error message as above.关闭文件后,我无法重新打开,并且收到与上述相同的错误消息。

For me, simply closing and restart VS Code did the trick.对我来说,只需关闭并重新启动 VS Code 就可以了。 Afterwards, the.ipynb-file opened as expected.之后,.ipynb 文件按预期打开。

notepad encoding format记事本编码格式

Resaving the document using another encoding format solved my problem.使用另一种编码格式重新保存文档解决了我的问题。 A regular.ipynb file (left image) is saved using Unix(LF) format, but the file that couldn't open was saved using UTF (right image)常规.ipynb 文件(左图)使用 Unix(LF) 格式保存,但无法打开的文件使用 UTF 保存(右图)

在此处输入图像描述

"Unable to open 'XXX.ipynb'" "Unexpected token < in JSON at position XXX" “无法打开‘XXX.ipynb’”“position XXX 处的意外令牌 < in JSON”

For me, I have similar issues when I am using git and reopen ipynb files in vscode.对我来说,当我使用 git 并在 vscode 中重新打开 ipynb 文件时,我遇到了类似的问题。

To fix it, pretty easy!要修复它,非常简单!

(1) Open and edit the file in json format and ACCEPT the current (1)打开并编辑json格式的文件,接受当前
changes or incoming changes.更改或传入的更改。

(2) Save and Close the edited file and reopen the file. (2) 保存并关闭编辑后的文件并重新打开文件。 Everything works fine!一切正常!

Good luck!祝你好运!

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 打开从 .txt 转换的 .ipynb 文件时出现非 JSON 错误 - NOT JSON ERROR when opening a .ipynb file converted from .txt 在python中发出http请求时,“ SyntaxError:JSON在位置0处出现意外令牌#” - “SyntaxError: Unexpected token # in JSON at position 0” when making a http request in python 在 VSCode 中打开 ipynb notebook 时出错 - Error while opening ipynb notebook in VSCode 打开 a.ipynb.txt 文件 - Opening a .ipynb.txt File JSON 中的意外令牌 D 位于位置 1 错误 - Unexpected token D in JSON at position 1 error SyntaxError: Unexpected token &lt; in JSON at position 0 如何解决 - SyntaxError: Unexpected token < in JSON at position 0 how to solve it 即使我从 Flask 后端返回 json,也在位置 0 处获取 JSON 中的意外令牌 &lt; - getting Unexpected token < in JSON at position 0 even when im returning json from flask backend 使用JSON.parse时,出现“ SyntaxError:JSON中位置1的意外令牌” - I am getting “SyntaxError: Unexpected token ' in JSON at position 1” when using JSON.parse vscode:如何自动缩进jupyter notebook ipynb文件? - vscode: how to autoindent jupyter notebook ipynb file? 发送 python 请求时,在 position 0 处的 JSON 中不断收到意外令牌 k - Keep getting an unexpected token k in JSON at position 0 when sending a python request
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM