【发布时间】:2021-11-04 13:05:14
【问题描述】:
我在 Windows 上使用搅拌机,按照教程:Visualize JSON Data in blender 但我在尝试打开 JSON 文件时卡住了。
我的代码(Windows):
import json
with open('C:\Users\Franktabs\Documents\export.json') as f:
j = json.load(f)
print(j)
他的代码(linux):
import json
with open('/home/chris/Downalds/tutorial.son') as f:
j = json.load(f)
print(j)
错误信息:
Python: File "C:\Users\Franktabs\Documents\Json2.blend\My Script", line 3
with open('C:\Users\Franktabs\Documents\export.json') as f:
^ SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape
location: <unknown location>:-1
尝试过的解决方案:
- 使用 import bpy
- 使用双\
- 使用“r”
- 使用 utf-8
- 检查文件类型其实是.json
- 在属性中检查文件的路径和名称
【问题讨论】:
-
这能回答你的问题吗? using backslash in python (not to escape)
-
字符串中的反斜杠(例如您的 Windows 路径)具有特殊含义,必须转义才能按字面意思使用。有关详细信息/解决方法,请参阅上面链接的问题的答案。
-
将反斜杠
\更改为正斜杠/。 -
@MattDMo 为什么?应该改用
os.path.join。 -
@OneCricketeer
os.path.join仍然需要一个基本路径,其中几乎肯定会包括\U...,这是触发 unicode 错误的原因。正斜杠在 Windows 路径文字中是完全可以接受的,并且 OP 声称已经尝试过\\和r文字(我怀疑这样做是否正确,因为它们失败了,但这是另一个话题)。这只是在不更改实际代码的情况下尝试的其他方法。