繁体   English   中英

IndentationError - 需要一个缩进的块

[英]IndentationError - expected an indented block

我得到 IndentationError: expected an indented block。 我试图将网页中的所有内容复制到 txt 文件中。 不确定错误是什么,但在我的作业中发现如果有空格和制表符的组合,则会出现缩进错误。 有人可以从 python 开始提供帮助。 提前致谢。

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预期的缩进块是跟随,所以这样做:

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

这与缩进ifelifelseforwhiletry等时的情况相同。

与许多其他语言不同,Python 使用缩进来创建代码块。 Python 代码块通常用 4 或 8 个空格缩进。 在复制/粘贴时,您应该注意您在网上找到的代码的缩进。 综上所述,从 Internet 上复制/粘贴随机代码是在实验室作业中获得病毒或 0 的好方法。

以下是有关 Python 缩进的更多详细信息: https : //www.python-course.eu/python3_blocks.php

暂无
暂无

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

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