代码简单但找不到错误(PyS60但不具体)
我是一个 Python 初学者,现在这让我很害怕:
L = []
file = urllib.urlopen("http://someurl.com/someText.txt")
line = file.readline()
while line != "" :
L.append(line)
line = file.readline()
appuifw.selection_list(choices=L)
我收到这个错误:
line = file.readline()
^
SyntaxError: invalid syntax
有人知道问题是什么吗?
I'm a Python beginner and now it's freakin me out:
L = []
file = urllib.urlopen("http://someurl.com/someText.txt")
line = file.readline()
while line != "" :
L.append(line)
line = file.readline()
appuifw.selection_list(choices=L)
and I get this error:
line = file.readline()
^
SyntaxError: invalid syntax
Does anyone know what the issue is?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
重写
它现在似乎可以工作。
(仍然存在问题,但我认为是 URL)
Rewriting to
it seems to work now.
(Still problems left but I think it's the URL)
显示看不见的东西。我敢打赌其中一行中隐藏着一个非法字符(null 是最喜欢的),并且它没有显示在您的屏幕上。或者文件的行尾类型可能错误。
我常用的技巧是:
1)你可能已经在 StackOverflow 中输入了它;尝试将此代码复制回源代码,看看它是否可以解决问题。有时很难看出是否将 ] 放在应该是 ) 或 } 的位置。
2)注释掉所有行,然后逐行取消注释,直到再次出现语法错误。如果当您注释掉所有其他行时存在语法错误,那么真正的问题出在上游。
3)删除有问题的行及其下方和上方的几行。通过一次操作删除这些行;你不希望坏角色留下来,因为它位于你一次删除的两行之间。然后重新输入这些行。不要将它们粘贴回去;这可能只是将问题粘贴回来。
Show the invisibles. I bet there is an illegal character (null is a favorite) hiding in one of those lines and it's not showing up on your screen. Or maybe the file has the wrong type of line ends.
My usual tricks here:
1) You might have typed it in right in StackOverflow; try copying this code back into the source and see if it fixes things. Sometimes it's hard to see if you put a ] where a ) or } should be.
2) Comment out all the lines, then uncomment them one at a time until the syntax error reappears. If the syntax error is there when you comment out all the other lines, then ther real problem is upstream.
3) Delete the line in question and a couple lines below and above it. Delete these lines in a single operation; you don't want the bad character to stay around because it was in between two lines that you deleted one at a time. Then retype those lines. Don't paste them back in; that might just paste the problem right back in.
您正在使用同名变量覆盖内置函数
file
。也许这会让 Py60 感到悲伤?You are overwriting the built-in function
file
with you variable of the same name. Maybe that causes the Py60 some grief?实际上我没有看到问题,除非您在缩进中混合制表符和空格,在这种情况下,错误应该抱怨缩进级别。但我想我应该指出,有一种更简洁的方法来读取类文件对象中的所有行:
I actually don't see a problem, unless you're mixing tabs and spaces in that indentation, in which case the error should complaining about indentation levels. But I thought I'd point out that there's a much cleaner way to read all the lines in a file-like object:
似乎在我的 Python 解释器(2.6.1)中工作正常。
我想你首先做了
import urllib
吗? (不这样做会导致NameError
,而不是SyntaxError
。)编辑:快速Google发现了这一点:http://discussion.forum.nokia.com/forum/showthread.php?t=150563
它已有 18 个月的历史,但它声称 PyS60 是 Python 2.2.2。我的机器上没有这个,但可能值得看看这是否是问题所在。
Seems to work fine in my Python interpreter (2.6.1).
I take it you did do
import urllib
first? (Not doing that would cause aNameError
, not aSyntaxError
.)EDIT: a quick Google found this: http://discussion.forum.nokia.com/forum/showthread.php?t=150563
It’s 18 months old, but it claims that PyS60 is Python 2.2.2. I don’t have that on my machine, but it might be worth seeing if that’s the issue.