简体   繁体   English

为什么在这种python2情况下re.sub不起作用?

[英]Why doesn't re.sub work in this python2 case?

import re
text = "PO 00000  Frm 00001  Fmt 0624  Sfmt 0634  E:\CR\FM\A07JN6.000  S07JNPT1"
text = re.sub(text, " ", text)

print(text)

I'm on Python 2.7.15. 我使用的是Python 2.7.15。 The output is PO 00000 Frm 00001 Fmt 0624 Sfmt 0634 E:\\CR\\FM\\A07JN6.000 S07JNPT1 . 输出为PO 00000 Frm 00001 Fmt 0624 Sfmt 0634 E:\\CR\\FM\\A07JN6.000 S07JNPT1 Why doesn't the output become " " ? 为什么输出不变成" "

Looks like you need re.escape 看起来您需要re.escape

Ex: 例如:

import re
text = "PO 00000  Frm 00001  Fmt 0624  Sfmt 0634  E:\CR\FM\A07JN6.000  S07JNPT1"
text = re.sub(re.escape(text), " ", text)

print(text)

Note: You can also use str.replace for this case. 注意:在这种情况下,您也可以使用str.replace

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM