Python Forum
SyntaxError: unexpected character after line continuation character
Thread Rating:
  • 2 Vote(s) - 3.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
SyntaxError: unexpected character after line continuation character
#1
Hello all, and I'm a new member here Big Grin .
I made a esoteric version of Python for fun, but I'm having some problems when running multiline code with the \n and \t characters. Here is the program that runs the code:
#QwertyPy
def decode(string):
    re = ""
    slist = string.split("_")
    for i in range(0,len(slist)):
        if slist[i][0] in "0123456789":
            if int(slist[i]) > 92:
                re = re + " "
            else:
                re = re + char[int(slist[i])]
        elif slist[i] not in char:
            re = re + slist[i]
    return re
char = "qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM0123456789~!@#$%^&*()_+-=[]{}\|;:,./<>? "
code = raw_input()
c = (decode(code))
exec(c)
Basically, it is Python but every character is mapped to the string "char" as seen above. Not sure how to explain this though. For example, a simple "Hello World!" program looks like this:
9_3_7_24_4_71_"_41_2_18_18_8_94_27_8_3_18_12_63_"_72
It separates the string by the "_" character, and gets the nth char in the char string. For example, the number "9" corresponds to the letter "p" in the char string, the number 3 to "r", and so on, and characters that aren't numbers are unchanged.

But I have trouble when entering this, which is an infinite counter:
7_103_76_123_52_81_24_1_15_7_18_2_147_30_3_6_2_84_81_24_81_4_9_3_7_24_4_71_7_72_81_24_81_4_7_138_74_76_113_53
Which corresponds to the normal Python:
i = 0
while True:
   print(i)
   i += 1
But for some reason it gives the following error:
Error:
Traceback (most recent call last): File "C:\Users\sakaf\Documents\Saka\scripts\Not Golly\qwertypy.py", line 17, in <module> exec(c) File "<string>", line 1 i = 0\nwhile True:\n\tprint(i)\n\ti += 1 ^ SyntaxError: unexpected character after line continuation character
Strangely enough, when I paste i = 0\nwhile True:\n\tprint(i)\n\ti += 1 and run it using exec() in the python shell, it runs fine. So why is this happening?

Thanks Big Grin
Reply
#2
That's some fun script.

To understand the problem you should print variable c.You'll see that i = 0\nwhile True:\n\tprint(i)\n\ti += 1 is printed which implies that the backslash has already been escaped. So your real string actually is i = 0\\nwhile True:\\n\\tprint(i)\\n\\ti += 1.

To solve the problem convert string to bytes and decode with unicode-escape

c = (decode(code))
c = c.encode('ascii').decode('unicode-escape')
exec(c)
More on problem:
>>> a = 'hello \nThis is newline \n\tThis is horizontal tab'
>>> print(a)
hello
This is newline
        This is horizontal tab
>>> a = 'hello \\nThis is not a newline \\n\\tThis is not a horizontal tab'
>>> print(a)
hello \nThis is not a newline \n\tThis is not a horizontal tab
Reply
#3
(Sep-25-2017, 02:28 PM)hbknjr Wrote: ...
To solve the problem convert string to bytes and decode with unicode-escape
...
Thank you so much! Now it works great and I can run pretty much any piece of python code in this obscure language. Thanks!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python rule about the space character surrounding the equal sign ineuw 10 1,515 Sep-21-2023, 09:17 AM
Last Post: ineuw
  How do I handle escape character in parameter arguments in Python? JKR 6 1,039 Sep-12-2023, 03:00 AM
Last Post: Apoed2023
Question UnicodeEncodeError: 'ascii' codec can't encode character u'\xe8' in position 562: ord ctrldan 23 4,605 Apr-24-2023, 03:40 PM
Last Post: ctrldan
  use of escape character in re.sub and find WJSwan 1 876 Feb-16-2023, 05:19 PM
Last Post: Larz60+
  Writing string to file results in one character per line RB76SFJPsJJDu3bMnwYM 4 1,305 Sep-27-2022, 01:38 PM
Last Post: buran
  UnicodeDecodeError: 'charmap' codec can't decode byte 0x81 in position 34: character Melcu54 7 18,308 Sep-26-2022, 10:09 AM
Last Post: Melcu54
  Unsupported Format Character Led_Zeppelin 2 5,183 Sep-20-2022, 01:46 PM
Last Post: deanhystad
  how to read chinese character? kucingkembar 2 1,312 Aug-25-2022, 08:42 PM
Last Post: kucingkembar
  How to split the input taken from user into a single character? mHosseinDS86 3 1,137 Aug-17-2022, 12:43 PM
Last Post: Pedroski55
  UnicodeEncodeError: 'ascii' codec can't encode character '\xfd' in position 14: ordin Armandito 6 2,644 Apr-29-2022, 12:36 PM
Last Post: Armandito

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020