Python error in Console but not in File: unexpected character after line continuation character -


i've got python script has class defined method:

@staticmethod def _sanitized_test_name(orig_name):     return re.sub(r'[`‘’\"]*', '', re.sub(r'[\r\n\/\:\?\<\>\|\*\%]*', '', orig_name.encode('utf-8'))) 

i'm able run script command prompt fine, without issues. when paste code of full class in console, syntaxerror: unexpected character after line continuation character:

>>> return re.sub(r'[`‘’\"]*', '', re.sub(r'[\r\n\/\:\?\<\>\|\*\%]*', '', orig_name.encode('utf-8')))   file "<stdin>", line 1     return re.sub(r'[``'\"]*', '', re.sub(r'[\r\n\/\:\?\<\>\|\*\%]*', '', orig_name.encode('utf-8')))                                                                                                     ^ syntaxerror: unexpected character after line continuation character 

if skip method while pasting, works. note there difference in original line , what's shown error: r'[`‘’\"]*' vs r'[``'"]*'. replacing ur'[`‘’"]*' gives syntaxerror: eol while scanning string literal.

it seems python console seeing stylised ` (backtick) , sytlised ' (single quote). when mean unicode open , close quotes. i've got # -*- coding: utf-8 -*- @ top of script, paste console well.

focusing on expression causing error r'[`‘’"]*'...

>>> r'[`‘’"]*'   file "<stdin>", line 1     r'[``'"]*'              ^ syntaxerror: eol while scanning string literal >>> ur'[`‘’"]*'  # unicode modifier   file "<stdin>", line 1     ur'[``'"]*'               ^ syntaxerror: eol while scanning string literal 

if terminal i'm in doesn't accept unicode input, interpretation of unicode chars ` , ', occurs.

so workaround split regex , use unichr() corresponding codes 2 quotes, 2018 , 2019:

>>> r'[`' + unichr(2018) + unichr(2019) + r'"]*' u'[`\u07e2\u07e3"]*' 

(and raw string modifier r'' isn't required particular regex.)


Comments

Popular posts from this blog

serialization - Convert Any type in scala to Array[Byte] and back -

matplotlib support failed in PyCharm on OSX -

python - Matplotlib: TypeError: 'AxesSubplot' object is not callable -