【发布时间】:2021-09-18 03:22:42
【问题描述】:
我想在 python 中运行以下命令。我需要打印'\0000'。我尝试不同的方式来打印它。我得到了“\x00”或“\000”。如何在 python 中生成字符串'\000'?非常感谢。
gsutil cp xxxxx.csv - | tr -d '\000' | gsutil cp - xxxxx.csv
>>> remove_string='''\000'''
>>> remove_string
'\x00'
>>> remove_string='\\000'
>>> remove_string
'\\000'
>>> remove_string=r'\\000'
>>> remove_string
'\\\\000'
>>> remove_string=r'\000'
>>> remove_string
'\\000'
【问题讨论】:
-
反斜杠(\)在字符串中有特殊含义,所以需要转义——
'\\000'或r'\000' -
我试过了,但还是不能得到\000
-
当您在 Python 交互模式下键入表达式时,您会看到值的
repr(),而不是其str()。'\\000'是你想要的字符串的repr()。 -
你想在这里做什么?
\000通常意味着一个空字节——你的代码看起来只是想从输入中删除空字节。在这种情况下,\x00或\0或\000都将是等效的。也许尝试为您打算如何使用此字符串提供更多上下文。 -
在
remove_string='\\000'之后尝试print(remove_string)。然后你得到包含的字符串,它是你想要的 4 个字符。 shell 打印repr(remove_string),它添加了引号和转义符,使字符串看起来像 python 文字。
标签: python python-3.x string