Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我正在尝试使用 shlex 拆分字符串,但我想保留(字面意思)“\t”、“\n”、“\r”。
例子:
text = "a=\t\n\r\example c=d" sp_text = shlex.split(text) print(sp_text) ['a=', 'example', 'c=d']
但我想打印这个:
['a=\t\n\r\example', 'c=d']
我可以使用 shlex 做到这一点吗?
/安杰洛斯
您可以执行以下操作:
import shlex text = "a=\t\n\r\example c=d" sh = shlex.shlex(text) sh.whitespace = ' ' sh.whitespace_split = True sp_text = list(sh) print(sp_text)
输出
['a=\t\n\r\\example', 'c=d']
请注意,上面的示例仅使用单个空格进行拆分,示例的想法是说明如何操作 shlex 空格。