Python 2.7.3 IndentationError:需要一个缩进块 - 找不到错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16533075/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python 2.7.3 IndentationError: expected an indented block - cant find mistake
提问by Sergey Samusev
I am new to python and trying to submit my HW on Coursera Data Science course. The environment there is VM running Python 2.7.3, the file tweet_sentiment.py I am trying to run has the following script within it:
我是 python 新手,并试图在 Coursera 数据科学课程上提交我的硬件。VM 运行 Python 2.7.3 的环境,我尝试运行的文件 tweet_sentiment.py 包含以下脚本:
import sys
import json
def hw():
print 'Hello, world!'
def lines(fp):
print str(len(fp.readlines()))
def main():
sent_file = open(sys.argv[1])
tweet_file = open(sys.argv[2])
# hw()
# lines(sent_file)
# lines(tweet_file)
myfile = open(sys.argv[1], 'r')
lines = myfile.readlines()
mydict = {}
for line in lines:
key, value = line.split("\t")
mydict[key] = int(value)
twit_file = open(sys.argv[2], 'r')
twit_lines = twit_file.readlines()
mylist = []
for line in twit_lines:
mylist.append(json.loads(line))
for listik in mylist:
twit_value = 0
twit_text = listik["text"]
twit_words = twit_text.split()
for word in twit_words:
if word in mydict:
twit_value = twit_value + 1
print float(twit_value)
if __name__ == '__main__':
main()
When running $ python tweet_sentiment.py I am getting the following error:
运行 $ python tweet_sentiment.py 时出现以下错误:
File "tweet_sentiment.py", line 25
key, value = line.split("\t")
^
IndentationError: expected an indented block
Thanks for any hints! Sergey
感谢您的任何提示!谢尔盖
回答by Xymostech
You have to indent the line after a forblock. Your code should look like:
您必须在for块后缩进该行。您的代码应如下所示:
for line in lines:
key, value = line.split("\t")
mydict[key] = int(value)
回答by matthewfisher
As it says, you have an indentation error. Line 25 should be corrected to this:
正如它所说,你有一个缩进错误。第 25 行应更正为:
def main():
...
for line in lines:
key, value = line.split("\t")
mydict[key] = int(value)
回答by fhelwanger
Your code must be like this:
你的代码必须是这样的:
for line in lines:
key, value = line.split("\t")
mydict[key] = int(value)
It's the same for all other for's.
对于所有其他 for 都是一样的。
回答by John La Rooy
Be careful! You're mixing tabs and spaces for indenting.
当心!您正在混合使用制表符和空格进行缩进。
Often a tab is displayed as the equivalent of 8 spaces. So, when using the common practice of 4 spaces it lookslike 2 levels of indentation, but is really only one.
通常一个制表符显示为相当于 8 个空格。因此,当使用 4 个空格的常见做法时,它看起来像是 2 级缩进,但实际上只有一级。
When I examine your code in the editor, I can see that you've got in at least two places. Replace those tabs with 4 spaces.
当我在编辑器中检查您的代码时,我可以看到您至少有两个地方。用 4 个空格替换这些制表符。
回答by kirbyfan64sos
Try:
尝试:
import sys
import json
def hw():
print 'Hello, world!'
def lines(fp):
print str(len(fp.readlines()))
def main():
sent_file = open(sys.argv[1])
tweet_file = open(sys.argv[2])
# hw()
# lines(sent_file)
# lines(tweet_file)
myfile = open(sys.argv[1], 'r')
lines = myfile.readlines()
mydict = {}
for line in lines:
key, value = line.split("\t")
mydict[key] = int(value)
twit_file = open(sys.argv[2], 'r')
twit_lines = twit_file.readlines()
mylist = []
for line in twit_lines:
mylist.append(json.loads(line))
for listik in mylist:
twit_value = 0
twit_text = listik["text"]
twit_words = twit_text.split()
for word in twit_words:
if word in mydict:
twit_value = twit_value + 1
print float(twit_value)
if __name__ == '__main__':
main()
The indentation for the forstatements is messed up. The forblock has to be indented. Also, as stated before, Python will throw an error if you mix tabs and spaces. Use either all spaces(replace each tab with 4 spaces) or all tabs(replace every 4 spaces with a tab).
for语句的缩进弄乱了。的for块必须被缩进。此外,如前所述,如果混合使用制表符和空格,Python 会抛出错误。使用所有空格(用 4 个空格替换每个制表符)或所有制表符(用制表符替换每 4 个空格)。
回答by Peng Zhang
Use vimto open it. And then type the commoand :retabIt then shows the line not correctly indented.
使用vim打开它。然后输入 commoand:retab它然后显示未正确缩进的行。

