shee
shee

Reputation: 145

remove leading null characters from string in python

I have a string with leading NUL characters I can see them as null when i open it in notepad++ else its shows as empty space, but any of the strip functions on string doesnot work how can I remove it?

exp=
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      {"asctime": "2021-09-07 18:58:37,645", "name": "Frontend_Tableau", "levelname": "DEBUG", "message": "Extracted Dashboard details from tableau", "type": "dashboard", "Name": "Overview"}

this is what i have tried

exp.lstrip()

Upvotes: 0

Views: 704

Answers (1)

jkoestinger
jkoestinger

Reputation: 1010

encode and decode should definitely work, I did the same tests as manjesh23:

>>> int(0).to_bytes(1, 'big')  # Creating a null byte (I don't know how yours is created in the first place)
b'\x00'

>>> test = int(0).to_bytes(1, 'big').decode('utf-8') + 'hello'  # Storing it as a string

>>> test
'\x00hello'

>>> print(test)
hello

>>> test.lstrip('\x00')
'hello'

>>> print(test.lstrip('\x00'))
hello

If it still doesn't work, we're missing some extra information I think

Upvotes: 1

Related Questions