Tutorial :How do I make these Perl regexs Python compatible?



Question:

I have these two lines in an old Perl script. When I write the Python equivalent I get all sorts of errors like valueerror: invalid \x escape, and stuff about encoding.

$line =~ s/[^\x{8}-\x{7B}]/ /ig;  $line =~ s/(Û|²|°|±|É|¹|Í)/ /g;  

What do I need to do to get them working in Python?


Solution:1

I'm not too great with Perl regex but I think I may have solved it:

invalid_range = re.compile(r'[^\x08-\x7B]', re.I)  invalid_unicode = re.compile(ur'(Û|²|°|±|É|¹|Í)')  line = re.sub(invalid_range , '', line)  line = re.sub(invalid_unicode, '', line)  


Solution:2

For encoding issues, if you want to put Unicode characters in your source directly, you'll need to make sure the Python interpreter knows what your file encoding is. See:


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »