简体   繁体   English

列出切片 json.loads 文件

[英]List slicing a json.loads file

What this code does it gets the values of a json.loads file.这段代码的作用是获取 json.loads 文件的值。 It gives me a list of dictionary that are organized by dates.它给了我一个按日期组织的字典列表。 This code works, my understanding is, i am taking the the first value in the list of dictionaries, so the first dictionary, but shouldn't.这段代码有效,我的理解是,我在字典列表中取第一个值,所以第一个字典,但不应该。 self.get_jsonparsed_data(self.ticker_text.get().upper())[0] work as well? self.get_jsonparsed_data(self.ticker_text.get().upper())[0]也可以吗? In my case it doesn't, I was hoping if someone can explain why I does not work.就我而言,它没有,我希望有人能解释为什么我不工作。

def get_jsonparsed_data(self, ticker):
    #quote
    url = (f"https://financialmodelingprep.com/api/v3/quote/{ticker}?)


    response = urlopen(url)
    data = response.read().decode("utf-8")
    return json.loads(data)

def search_info(self):
    #self.info.delete(0, END)

    recent_filing = []

    for header in self.get_jsonparsed_data(self.ticker_text.get().upper())[:1]:
        recent_filing.append(header)

    ticker = self.ticker_text.get()

    #output dictionary values with proper format
    try:
        recent_filing_dict = recent_filing[0]

This works.这行得通。 I get the first dictionary which is what i want but when i do self.get_jsonparsed_data(self.ticker_text.get().upper())[0] instead of self.get_jsonparsed_data(self.ticker_text.get().upper())[:1] it gives me an error我得到了我想要的第一本字典,但是当我做self.get_jsonparsed_data(self.ticker_text.get().upper())[0]而不是self.get_jsonparsed_data(self.ticker_text.get().upper())[:1]它给我一个错误

which pretty much is saying there isnt any values appended to recent_filing_dict.这几乎是在说 recent_filing_dict 没有附加任何值。 I was just hoping if someone can explain why?我只是希望有人能解释为什么?

"for" loops through iterable and self.get_jsonparsed_data(self.ticker_text.get().upper())[0] seemingly returns an item rather than iterable (list) while self.get_jsonparsed_data(self.ticker_text.get().upper())[:1] returns an iterable (single item list) which is iterated over by for loop “for”循环遍历可迭代的和self.get_jsonparsed_data(self.ticker_text.get().upper())[0]似乎返回一个项目而不是可迭代的(列表),而self.get_jsonparsed_data(self.ticker_text.get().upper())[:1]返回一个可迭代的(单项列表),它由 for 循环迭代

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

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