Unable to find null bytes in Python code in Pycharm?

2024/11/16 2:23:59

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.pyFile "helloworld.py", line 1print('Hello world!^
SyntaxError: EOL while scanning string literalλ cat -A helloworld.py
print('Hello world!^@')
Answer

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.

Finding NUL byte in HxD

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

https://en.xdnf.cn/q/119582.html

Related Q&A

remove single quotes in list, split string avoiding the quotes

Is it possible to split a string and to avoid the quotes(single)? I would like to remove the single quotes from a list(keep the list, strings and floats inside:l=[1,2,3,4.5]desired output:l=[1, 2, 3, …

Image Segmentation to Map Cracks

I have this problem that I have been working on recently and I have kind of reached a dead end as I am not sure why the image saved when re opened it is loaded as black image. I have (original) as my b…

operations on column length

Firstly, sorry if I have used the wrong language to explain what Im operating on, Im pretty new to python and still far from being knowledgeable about it.Im currently trying to do operations on the len…

Python: Parse one string into multiple variables?

I am pretty sure that there is a function for this, but I been searching for a while, so decided to simply ask SO instead.I am writing a Python script that parses and analyzes text messages from an inp…

How do I pull multiple values from html page using python?

Im performing some data analysis for my own knowledge from nhl spread/betting odds information. Im able to pull some information, but Not the entire data set. I want to pull the list of games and the a…

Creating h5 file for storing a dataset to train super resolution GAN

I am trying to create a h5 file for storing a dataset for training a super resolution GAN. Where each training pair would be a Low resolution and a High resolution image. The dataset will contain the d…

How to resolve wide_to_long error in pandas

I have following dataframeAnd I want to convert it into the following format:-To do so I have used the following code snippet:-df = pd.wide_to_long(df, stubnames=[manufacturing_unit_,outlet_,inventory,…

Odoo 10: enter value in Many2one field dynamically

I added in my models.py :commercial_group = fields.Many2one("simcard.simcard")and in my views.xml :<field name="commercial_group" widget="selection"/>And then i am t…

How to erode this thresholded image using OpenCV

I am trying to first remove the captcha numbers by thresholding and then eroding it ,to get slim continuous lines to get better output. Problem:the eroded image is not continuous as u can see Original …

Searching for only the first value in an array in a csv file

So i am creating a account login system which searches a database for a username (and its relevant password) and, if found, will log the user on.This is what the csv file currently looks like[dom, ente…