我目前面临一个奇怪的问题。 我想用“null”替换字符串中的“\0”,并阅读了许多论坛,总是看到相同的答案:
text_it = "request on port 21 that begins with many '\0' characters,
preventing the affected router"
text_it.replace('\0', 'null')
或
text_it.replace('\x00', 'null')
当我现在打印字符串时,我得到以下结果:
"request on port 21 that begins with many '\0' characters, preventing the
affected router"
什么也没发生。
所以我使用了这个方法,它确实有效,但对于这么小的改变来说似乎需要付出太多的努力:
text_it = text_it.split('\0')
text_it = text_it[0] + 'null' + text_it[1]
知道为什么替换功能不起作用吗?
最佳答案
一行:
text_it = text_it.replace('\0', 'null').replace('\x00', 'null')
关于Python将字符串中的 '\0'替换为null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51782114/