简体   繁体   English

python 中的 IndentationError - 我的代码有什么问题?

[英]IndentationError in python - what is wrong in my code?

import requests
import json

url='https://www.nseindia.com/api/option-chain-indices?symbol=NIFTY'    
headers= {'user-agent':'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.162 Safari/537.36',
"accept-language":"en-US,en;q=0.9","accept-encoding":"gzip, deflate"} 
def fetch_oi():  
r=requests.get(url,headers=headers).json()
print(r)
with open("oidata.json","w") as files:
    files.write(json.dumps(r,indent=4, sort_keys=True))

def main():
    fetch_oi()


if __name__=='__main__':
    main()

** in this code i'm getting error in r=requests.get(url,headers=headers).json() ^ IndentationError: expected an indented block ** 在这段代码中,我在 r=requests.get(url,headers=headers).json() 中遇到错误 ^ IndentationError: expected an indented block

what i'm doing wronge?我在做什么错? plzz help请帮忙

The code in a function definition needs to be indented.函数定义中的代码需要缩进。

import json

url='https://www.nseindia.com/api/option-chain-indices?symbol=NIFTY'    
headers= {'user-agent':'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.162 Safari/537.36',
"accept-language":"en-US,en;q=0.9","accept-encoding":"gzip, deflate"} 

def fetch_oi():  
    r=requests.get(url,headers=headers).json()
    print(r)
    with open("oidata.json","w") as files:
        files.write(json.dumps(r,indent=4, sort_keys=True))

def main():
    fetch_oi()


if __name__=='__main__':
    main()

Python depends heavily on correct indentation, see: https://www.w3schools.in/python-tutorial/concept-of-indentation-in-python/ Python 在很大程度上依赖于正确的缩进,请参阅: https : //www.w3schools.in/python-tutorial/concept-of-indentation-in-python/

After your def fetch_oi(): you are supposed to indent the line r=requests.get(url,headers=headers).json() since it is a function在你def fetch_oi():你应该缩进r=requests.get(url,headers=headers).json()因为它是一个函数

def fetch_oi():  
    r=requests.get(url,headers=headers).json()
    print(r)

then depending on where your function ends, you should keep indenting然后根据您的功能结束的位置,您应该继续缩进

I believe it is clear that you need我相信很明显你需要

def fetch_oi():  
    r=requests.get(url,headers=headers).json()
    print(r)
    with open("oidata.json","w") as files:
        files.write(json.dumps(r,indent=4, sort_keys=True))

instead of what you have (that is one tab more in fetch_io).而不是你所拥有的(在 fetch_io 中又多了一个标签)。 In python you have to be careful with tabs.在 python 中,你必须小心使用标签。 You need one identation level after each function.每个函数后都需要一个标识级别。 Take a look here: https://docs.python.org/2.0/ref/indentation.html看看这里: https : //docs.python.org/2.0/ref/indentation.html

The body of your function fetch_oi is unindented.函数fetch_oi的主体是无缩进的。

import requests
import json

url='https://www.nseindia.com/api/option-chain-indices?symbol=NIFTY'    
headers= {'user-agent':'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.162 Safari/537.36', "accept-language":"en-US,en;q=0.9","accept-encoding":"gzip, deflate"} 

def fetch_oi():  
    r=requests.get(url,headers=headers).json()
    print(r)

    with open("oidata.json","w") as files:
        files.write(json.dumps(r,indent=4, sort_keys=True))

def main():
    fetch_oi()

if __name__=='__main__':
    main()

def fetch_oi() 之后的意图行:

Issue is in the body of问题在正文中

def fetch_oi(): 

Put space to the body为身体留出空间

My suggestions is to use sublime IDE.我的建议是使用 sublime IDE。 So, when you select the content you can visible the content that you have used space or tab.因此,当您选择内容时,您可以看到已使用空格或标签的内容。 Also using tab is the good approach rather than space同样使用制表符是好方法而不是空格

I have seen that you have used space.我已经看到你使用了空间。 So adjusting and using space will make you troublesome at long run.所以从长远来看,调整和使用空间会让你很麻烦。

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

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