-1

How to remove NUL characters / white space in string? I've tried to use the entire answer and comments here. Seems none of them work for me.

For example the string is m o d e m

 for part_entry in part_list:
        partStr = unicode(part_entry[5])
        partStr.rstrip(' \t\r\n\0')
        logging.debug('******')
        logging.debug('PartName= '+partStr)

This is what currently I compile.

I tried to log it, and it's rendering output like this;

Python NUL logs

Bianca
  • 973
  • 2
  • 14
  • 33
  • 1
    GREAT !! no nothing no comment just VOTE DONW ! – Bianca Feb 16 '18 at 22:32
  • Hm.... You do know what `rstrip` does? – Jongware Feb 16 '18 at 22:35
  • That was my last attempt. left, right. I am desperate now :( – Bianca Feb 16 '18 at 22:37
  • 1
    Well, a NUL in the middle is not left nor right is it? But I am unsure about your comment "white space". Your own attempt correctly (!) lists `\t`, `\r`, `\n`, and `\0` but leaves out the space itself. Should it also be removed? Would you not rather *replace* these characters with a correct single space? – Jongware Feb 16 '18 at 22:39
  • My problem is `space` and that `NUL` on the image logs "PartName" exactly on that log image attached – Bianca Feb 16 '18 at 22:44
  • Ah wait. Your `\r\n\t` sequence does not have anything to do with this. You must be reading a *Unicode 16-bit encoded* text file. Here is a bit of good news, then: Python is perfectly able to read that correctly. Where does that string come from? Indeed from a file? – Jongware Feb 16 '18 at 22:47
  • The string is not from a file. It was an output of a result. `partStr = unicode(part_entry[5])`. Please have a look on the URL. – Bianca Feb 16 '18 at 22:54

2 Answers2

2

Try this:

content = []
with open('file.ext', as 'r') as file_in:
    content = file_in.readlines()

new_content = [] 
for line in file:
    clean_line = line.replace('\00', '')
    new_content.append(clean_line)

with open('outfile.ext', 'w') as file_out:
    file_out.writelines(new_content)
Matt_G
  • 506
  • 4
  • 14
  • I think i can not do it this way, this was not from a text file. The string is come from a result / procedure – Bianca Feb 16 '18 at 22:53
  • 1
    @Bianca Where it comes from is irrelevant, you remove the characters the same way. – Barmar Feb 16 '18 at 23:49
1

As @Matt_G mentioned, you can replace characters in a string with str.replace(old, new). This method is available in python 2.7 and 3.x.

To cite the documentation for str.replace(old, new, [count]): 'Return a copy of the string with all occurrences of substring old replaced by new. If the optional argument count is given, only the first count occurrences are replaced.' This means you can use str.replace for replacing the substring \x00, aka NUL, in your string with this method.

Since you wanted a solution for a returned value, see below:

def work():
    return "Foo \0"

result = work()

print(result) # -> 'Foo NUL'
print(result.replace('\0', 'Bar')) # -> 'Foo Bar'

Another example with the code from your answer (Example is for python 2, since unicode method was used):

for part_entry in part_list:
    partStr = unicode(part_entry[5]).replace('\0', '')
    partStr.rstrip(' \t\r\n\0')
    logging.debug('******')
    logging.debug('PartName= ' + partStr)

To directly remove NUL, alias \0 or \00, line-by-line, please see the answer from Matt_G.

If you want to remove multiple characters at once (guessing based on the usage of str.rstrip), please use str.translate (docs: 2.7 | 3.9) instead (also for python 2 | See this SO post for the usage of translate for unicode strings):

for part_entry in part_list:
    partStr = unicode(part_entry[5]).translate({
        ord('\t'): None,
        ord('\r'): None,
        ord('\n'): None,
        ord('\0'): None
    })
    logging.debug('******')
    logging.debug('PartName= ' + partStr)

I hope this helped you and if they're any open questions feel free to ask right away.

PS: Learn more about the NUL character

Cobalt
  • 447
  • 5
  • 9