html tool

2019年8月1日星期四

sre_constants.error: bogus escape (end of line)

问题: re.sub 出现 sre_constants.error: bogus escape (end of line)

解决:

 1 def raw_string(s):                                                                                 
  2     if isinstance(s, str): #函数来判断一个对象是否是一个已知的类型,类似 type(),与type 不同考虑继承。
  3         print "isinstance_str"                                                                     
  4         s = s.encode('string-escape')                                                              
  5     elif isinstance(s, unicode):                                                                   
  6         print "isinstance_unicode"                                                                 
  7         s = s.encode('unicode-escape')                                                             
  8     return s                                                                                       
  9                                                                                                    
 10 import re                                                                                          
 11                                                                                                    
 12 s = "This \\"                                                                                      
 13 t = re.sub("this", raw_string(s), "this is a text")                                                
 14 print t                                                                                            
 15 t1 = re.sub("this", s, "this is a text")                                                           
 16 print t1        

没有评论:

发表评论