简体   繁体   English

行后续字符后的Python意外字符

[英]Python unexpected character after line continuation character

I am very new to Python. 我是Python的新手。 I am constructing a string that is nothing but a path to the network location as follows. 我正在构建一个字符串,它只是一个网络位置的路径,如下所示。 But it outputs the error: "Python unexpected character after line continuation character". 但它输出错误:“行后续字符后的Python意外字符”。 Please help. 请帮忙。 I saw this post but I am not sure if it applies to my scenario: 我看到这篇文章,但我不确定它是否适用于我的场景:

syntaxerror: "unexpected character after line continuation character in python" math syntaxerror:“python中的行继续符后面的意外字符”数学

s_path_publish_folder = r"\\" + s_host + "\" + s_publish_folder "\" + s_release_name

One of your \\ backslashes escapes the " double quote following it. The rest of the string then ends just before the next \\ backslash, and that second backslash is seen as a line-continuation character. Because there's another " right after that you get your error: 你的一个\\反斜杠会转义 "跟随它的双引号。其余的字符串然后在下一个\\反斜杠之前结束 ,第二个反斜杠被视为一个行继续符。因为还有另一个"之后你得到你的错误:

s_path_publish_folder = r"\\" + s_host + "\" + s_publish_folder "\" + s_release_name
#                                         ^^ not end of string   ||
#                                        ^--- actual string  ---^||
#                                              line continuation /|
#                                                 extra character /   

You need to double those backslashes: 你需要加倍反斜杠:

s_path_publish_folder = r"\\" + s_host + "\\" + s_publish_folder "\\" + s_release_name

Better yet, use the os.path module here; 更好的是,在这里使用os.path模块; for example, you could use os.path.join() : 例如,您可以使用os.path.join()

s_path_publish_folder = r"\\" + os.path.join(s_host, s_publish_folder, s_release_name)

or you could use string templating: 或者你可以使用字符串模板:

s_path_publish_folder = r"\\{}\{}\{}".format(s_host, s_publish_folder, s_release_name)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM