syntaxerror: unexpected character after line continuation character in python

后端 未结 3 1031
别跟我提以往
别跟我提以往 2020-12-11 22:48

Can anybody tell me what is wrong in this program? I face

syntaxerror unexpected character after line continuation character

when I run this

相关标签:
3条回答
  • 2020-12-11 23:31

    You need to quote that filename:

    f = open("D\\python\\HW\\2_1 - Copy.cp", "r")
    

    Otherwise the bare backslash after the D is interpreted as a line-continuation character, and should be followed by a newline. This is used to extend long expressions over multiple lines, for readability:

    print "This is a long",\
          "line of text",\
          "that I'm printing."
    

    Also, you shouldn't have semicolons (;) at the end of your statements in Python.

    0 讨论(0)
  • 2020-12-11 23:43

    Replace

    f = open(D\\python\\HW\\2_1 - Copy.cp,"r");

    by

    f = open("D:\\python\\HW\\2_1 - Copy.cp", "r")

    1. File path needs to be a string (constant)
    2. need colon in Windows file path
    3. space after comma for better style
    4. ; after statement is allowed but fugly.

    What tutorial are you using?

    0 讨论(0)
  • 2020-12-11 23:48

    The filename should be a string. In other names it should be within quotes.

    f = open("D\\python\\HW\\2_1 - Copy.cp","r")
    lines = f.readlines()
    for i in lines:
        thisline = i.split(" ");
    

    You can also open the file using with

    with open("D\\python\\HW\\2_1 - Copy.cp","r") as f:
        lines = f.readlines()
        for i in lines:
            thisline = i.split(" ");
    

    There is no need to add the semicolon(;) in python. It's ugly.

    0 讨论(0)
提交回复
热议问题