无法在 Pycharm 的 Python 代码中找到空字节? [英] Unable to find null bytes in Python code in Pycharm?

查看:79
本文介绍了无法在 Pycharm 的 Python 代码中找到空字节?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在复制/粘贴代码期间,我经常在 Python 代码中得到 null 字节.Python 本身报告针对模块的一般错误并且不指定空字节的位置.我选择的 IDE 像 PyCharm,没有对此进行检查,也没有在代码中看到空字节.

那么,在 Python 代码中查找 null 字节的一般方法是什么?

<小时>

λ cat main.py导入你好世界λ cat helloworld.py打印('你好世界!')λ python main.py回溯(最近一次调用最后一次):文件main.py",第 1 行,在 <module> 中导入你好世界ValueError:源代码字符串不能包含空字节λ python helloworld.py文件helloworld.py",第 1 行打印('你好世界!^语法错误:扫描字符串文字时 EOLλ cat -A helloworld.pyprint('你好世界!^@')

解决方案

该问题很可能是由复制/粘贴引起的,因为很难在键盘上键入这样的字符.

<块引用>

那么,在 Python 代码中查找空字节的一般方法是什么?

恕我直言,无需每次都检查源代码中的 NUL 字符.我使用源代码已经超过 10 年了,我从来没有遇到过这样的复制/粘贴问题,即使我对许多原型和 MCVE 使用复制/粘贴.

因此,检查源代码的触发器正是一条错误消息,例如来自 Python 的错误消息.

使用哪种工具?十六进制编辑器,因为它将显示 NUL 字符.对于 Windows,我选择的是免费软件

在 Linux 上,命令 grep -Pa '\x00' helloworld.py 应该没问题.

During copy/pasting code I often get null bytes in Python code. Python itself reports general error against module and doesn't specify location of null byte. IDE of my choice like PyCharm, doesn't have check for this and doesn't see null bytes in code.

So, what is the general way to find null bytes in Python code?


λ cat main.py
import helloworld
λ cat helloworld.py
print('Hello world! ')
λ python main.py
Traceback (most recent call last):
  File "main.py", line 1, in <module>
    import helloworld
ValueError: source code string cannot contain null bytes

λ python helloworld.py
  File "helloworld.py", line 1
    print('Hello world!
                      ^
SyntaxError: EOL while scanning string literal

λ cat -A helloworld.py
print('Hello world!^@')

解决方案

The issue is likely caused by copy/paste, since it's hard to type such a character on the keyboard.

So, what is the general way to find null bytes in Python code?

IMHO there's no need to check for NUL chcracters in your source code every time. I'm working with source code for >10 years now, and I've never had such a copy/paste issue, even I use copy/paste for many prototypes and MCVEs.

So, the trigger to check the source code is exactly an error message such as the one from Python.

Which tool to use? A hex editor, since it will display the NUL character. For Windows, my choice is the freeware HxD.

On Linux, the command grep -Pa '\x00' helloworld.py should be fine.

这篇关于无法在 Pycharm 的 Python 代码中找到空字节?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆