0

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()
5
  • What if you do exp = exp.decode('utf-8').lstrip('\x00').encode('utf-8'). Also, don't forget that lstrip is not modifying the value in-place, so you have to reassign it (like exp = exp.lstrip) Commented Sep 7, 2021 at 13:49
  • 1
    exp.lstrip() This is actually working. >>> >>> exp = " Manjesh" >>> exp ' Manjesh' >>> exp.lstrip() 'Manjesh' Tested on Python 3.9 Terminal and works great Commented Sep 7, 2021 at 13:51
  • @jkoestinger exp is a string type so decode is not supported can I try exp=exp.lstrip('\x00') is it correct? Commented Sep 7, 2021 at 13:52
  • I mixed up encode and decode, they should be inverted, but manjesh23 seems to have tried directly Commented Sep 7, 2021 at 13:54
  • @jkoestinger encode and decode not working for str obj Commented Sep 7, 2021 at 13:57

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.