在复制/粘贴代码期间,我经常在Python代码中获得null
字节。 Python本身报告针对模块的一般错误,并且不指定空字节的位置。我选择的IDE如PyCharm,没有检查这个并且在代码中看不到空字节。
那么,在Python代码中查找null
字节的一般方法是什么?
λ 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!^@')
问题很可能是由复制/粘贴引起的,因为在键盘上输入这样的字符很困难。
那么,在Python代码中找到空字节的一般方法是什么?
恕我直言,每次都不需要在源代码中检查NUL个字符。我正在使用源代码超过10年,而且我从未遇到过这样的复制/粘贴问题,即使我使用复制/粘贴许多原型和MCVE。
因此,检查源代码的触发器正是错误消息,例如来自Python的错误消息。
使用哪种工具?十六进制编辑器,因为它将显示NUL字符。对于Windows,我的选择是免费软件HxD。
在Linux上,命令grep -Pa '\x00' helloworld.py
应该没问题。