0

So i got custom database in python that looks like this

{"1":"0xAD488499b897a9667395526C39d0f3314A0f3FE9","2":"0xAD48843b897449667395526C39d0f3314A0f3FE9"}

And I want to extract only addresses I got other database that keeps count of how many addresses they are

db = DB("db/addrtouser.db")
db1 = DB("db/addrstorage.db")
db2 = DB("db/number.db")

    how = db2.get("how") # how many addreses they are

    checkaddr = #only second addreses
    for element in checkaddr:
        print('check')
        json = await tools.getonl(element)
        if json == False:
            author = int(db.get(element))
            owner = await client.fetch_user(author)  # your user ID
            await owner.send("YOUR NODE IS NOT ONLINE!")    

I want checkaddr to be "0xAD488499b897a9667395526C39d0f3314A0f3FE9","0xAD48843b897449667395526C39d0f3314A0f3FE9" So to just extract 2nds from the database with some while or for loop

Carles Mitjans
  • 4,786
  • 3
  • 19
  • 38
  • What varibable type is `how`? If it is a simple dictionary you can use `how.values()` to access its values – Carles Mitjans Jul 06 '21 at 12:13
  • @CarlesMitjans its a int {"how": 2} – Toni Dumancic Jul 06 '21 at 12:17
  • I think what we need is the type of db, db1, db2. Hopefully they are dictionaries. If so, see https://stackoverflow.com/questions/16228248/how-can-i-get-list-of-values-from-dict (shows how for Python versions 2 and 3) – Basya Jul 06 '21 at 12:43
  • Also, your title says you want to extract keys, but in your question it appears that you want to extract a list of the values, without the keys. Which one are you trying to do? – Basya Jul 06 '21 at 12:46
  • @Basya so i want to from ```{1:a,2:b}`` to get '''[a,b]``` – Toni Dumancic Jul 06 '21 at 12:57
  • Well, that looks like going from a dict to a list; take a look at the question I linked to -- that may have the answer you need, ready-made. – Basya Jul 06 '21 at 12:59
  • Oh, and (I should have started with this!) Welcome to Stack Overflow! – Basya Jul 06 '21 at 13:00
  • @Basya im getting error – Toni Dumancic Jul 06 '21 at 13:01
  • https://pastebin.com/9QBexE7z – Toni Dumancic Jul 06 '21 at 13:01
  • I'm sorry you are getting an error and would be happy to help you try to solve it, but I'll need a bit more detailed information. What have you tried? What error are you getting? What are the types of your variables? – Basya Jul 06 '21 at 13:02
  • OK, you are trying to get the values from checkaddr, but checkaddr is not a dict. I thought you wanted to get the values from your database? – Basya Jul 06 '21 at 13:03
  • @Basya so i have func that reads from json db, ``` checkaddr = db1.dumpdb()# second addreses``` but its saying that data dump is bool – Toni Dumancic Jul 06 '21 at 13:04
  • ``` File "main.py", line 22, in test1 checkaddr1 = list(checkaddr.values()) AttributeError: 'bool' object has no attribute 'values'`` – Toni Dumancic Jul 06 '21 at 13:05
  • Well then I guess you'll have to find out more about the database types and functions you are using. I don't know what they are, or from what library (if any) they come. – Basya Jul 06 '21 at 13:05
  • I use this custom database https://www.freecodecamp.org/news/how-to-write-a-simple-toy-database-in-python-within-minutes-51ff49f47f1/ – Toni Dumancic Jul 06 '21 at 13:06
  • Well, if so, it is clear why is saying that the return of dumpdb is bool: def dumpdb(self): try: json.dump(self.db , open(self.location, "w+")) return True except: return False – Basya Jul 06 '21 at 13:12
  • For us to help you you first need to do some homework. Figure out what the types are of your variables, and which exact one has the information you want to work with. Then we can help you figure out how to do it. We can not figure out for you what you already have. If you can provide us with a minimal reproducible example (https://stackoverflow.com/help/minimal-reproducible-example) we will be best able to help you. – Basya Jul 06 '21 at 13:13
  • @Basya fixed just add get all and its fixed – Toni Dumancic Jul 06 '21 at 13:14
  • Sorry for my bad english sorry – Toni Dumancic Jul 06 '21 at 13:14
  • Glad you worked it out; I am still not clear on what you had and what you did but I don't need it anymore... And, bad English is no crime; I think I understood your English OK. – Basya Jul 06 '21 at 13:17

1 Answers1

0

To get all the values from a dictionary, do this:

my_dict = {"first_key": 1, "second_key": 2}
print(list(my_dict.values()))

This will print:

[1, 2]
Enfors
  • 960
  • 2
  • 14
  • 25
  • This duplicates the answers to the question linked to in the comments on the question, https://stackoverflow.com/questions/16228248/how-can-i-get-list-of-values-from-dict. – Basya Jul 06 '21 at 18:32