简体   繁体   中英

Counting occurences in a Json with python

i managed to find a way to scrap discord messages from a specific channel, turn them into a json format and print the result.

import requests
import json

    def retrieve_messages(channelid):
      headers = {
        'authorization':'MjExMTQ1ODEzNTk2ODMxNzU2.YjtnkQ.-jOz1PucvMjdQ8r2yXdKMpSLK2M'
      }
      response = requests.get(
       f'https://discord.com/api/v9/channels/{channelid}/messages', headers=headers)
      data = json.loads(response.text)
      for description in data:
        print(description)
    
    retrieve_messages('956262565237903401')

Which returns just fine what it's supposed to.

EG: Cleared in 27:38 of 38:00 (27.3% remaining) for 148 Points\n\n Kindama - Healer (Holy Priest)

The thing that i'd like to do and cannot find how to whatsoever, is, how can i count the occurences in all those lines of a particular keyword such as "Kindama"

I've tried syntax with.index which return false even if the string is present in json,

If anyone with python/json knowledge could give me hints about where to go from there i'd be thankful,

EDIT, Tryouts:

adding to the function:

index=data.index("Kindama")
print(index)

Tells me that 'Kindama' is not in list

adding to the function:

for line in data:
      for key in occurences.keys():
          occurences[key] += line.count(key)        
print(occurences)

Tells me that "AttributeError: 'dict' object has no attribute 'count'"

So what i'm guessing at that point is that the scrapping didnt transform the data in a proper string or that i'm accessing it properly

You can use a dictionary and string.count(substring) , like so:

text = [
    " Cleared in 27:38 of 38:00 (27.3% remaining) for 148 Points\n\n🛡 Kindama - Healer (Holy Priest)",
    "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.",
    "At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet."
    "Lorem, ipsum, ipsum"
]

occurences = {
    "Kindama" : 0,
    "Lorem" : 0,
    "ipsum" : 0,
    "Nope" : 0
}

for line in text:
    for key in occurences.keys():
        occurences[key] += line.count(key)
        
print(occurences)

Result:

{'Kindama': 1, 'Lorem': 3, 'ipsum': 4, 'Nope': 0}

Notice that the solution is case sensitive

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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