简体   繁体   English

IndentationError - 需要一个缩进的块

[英]IndentationError - expected an indented block

I get the IndentationError: expected an indented block.我得到 IndentationError: expected an indented block。 I was trying to copy all the contents form webpage to a txt file.我试图将网页中的所有内容复制到 txt 文件中。 Not sure what the error is but in my homework found that indented error occure if there is a combination of space and tabs.不确定错误是什么,但在我的作业中发现如果有空格和制表符的组合,则会出现缩进错误。 Beginning with python can someone help.有人可以从 python 开始提供帮助。 Thanks in Advance.提前致谢。

import requests
url = 'https://seekingalpha.com/article/4166013-t-t-q1-2018-results-earnings-call-transcript?part=single'
data = requests.get(url)
with open('file.txt','w') as out_f:
out_f.write(data.text.encode('utf-8'))

with expects an indented block is to follow, so do this: with预期的缩进块是跟随,所以这样做:

with open('file.txt','w') as out_f:
    out_f.write(data.text.encode('utf-8'))

This is the same as when you indent for an if , elif , else , for , while , try , etc.这与缩进ifelifelseforwhiletry等时的情况相同。

Unlike many other languages Python uses indentation to create code blocks.与许多其他语言不同,Python 使用缩进来创建代码块。 Python code blocks are usually indented with 4 or 8 spaces. Python 代码块通常用 4 或 8 个空格缩进。 You should pay attention to the indentation of the code you find online when copy/pasting.在复制/粘贴时,您应该注意您在网上找到的代码的缩进。 All that said, copy/pasting random code off the Internet is a good way to get a virus or a 0 on a lab assignment.综上所述,从 Internet 上复制/粘贴随机代码是在实验室作业中获得病毒或 0 的好方法。

Here is some more details about Python indentation: https://www.python-course.eu/python3_blocks.php以下是有关 Python 缩进的更多详细信息: https : //www.python-course.eu/python3_blocks.php

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

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