语法错误:python中的行继续符后出现意外字符 [英] syntaxerror: unexpected character after line continuation character in python

查看:134
本文介绍了语法错误:python中的行继续符后出现意外字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以告诉我该程序有什么问题吗?我面对

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

syntaxerror unexpected character after line continuation character

当我运行此程序时:

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

for i in lines:  
    thisline = i.split(" ");

推荐答案

您需要引用该文件名:

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

否则,D后面的反斜杠将被解释为换行符,并应在换行符之后.这是为了将长表达式扩展到多行上,以提高可读性:

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."

此外,在Python语句的末尾不应使用分号(; ).

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

这篇关于语法错误:python中的行继续符后出现意外字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆