-3

how can i change the dict values which digit to int and have no quotes

User input this string

{"name":"John", "age":30, "city":"New York"}

what ive tried

t=[x for x in input()[2:-1].split(", ")]
w=[]
for i in t:
    a=i
    e=a.replace("'","").replace('"','').split(":")
    w.append(e)
for i in w:
    for j in i:
        if j.isdigit():
            j=int(j)
d=[tuple(x) for x in w]
print(d)

It shows that 30 type int but it has quotes

Output

[('name', 'John'), ('age', '30'), ('city', 'New York')]

I want 30 to be without quotes

[('name', 'John'), ('age', 30), ('city', 'New York')]
Aha
  • 15
  • 6
  • Seems like it would be easier to start by parsing the string with `ast.literal_eval` and then get its items. – khelwood Jan 22 '22 at 12:06
  • Why do you have to go through all of that code? all you have to do is `list(mydict.items())` simpler – Avishka Dambawinna Jan 22 '22 at 12:09
  • @khelwood im beginner and i would like to use more simple things – Aha Jan 22 '22 at 12:10
  • @AvishkaDambawinna its not dict at beginnig its a string – Aha Jan 22 '22 at 12:13
  • oh, the user inputs a string :') – Avishka Dambawinna Jan 22 '22 at 12:13
  • 2
    Does this answer your question? [Convert a String representation of a Dictionary to a dictionary?](https://stackoverflow.com/questions/988228/convert-a-string-representation-of-a-dictionary-to-a-dictionary) – Avishka Dambawinna Jan 22 '22 at 12:14
  • @AvishkaDambawinna i dont know almost every task like that for us user inputs smt – Aha Jan 22 '22 at 12:16
  • @AvishkaDambawinna my bad i shoudve add to question no imports for beginner who doesnt know imports – Aha Jan 22 '22 at 12:21
  • All you need to do is change the type of the `30` to `int` when you're making the list of tuples(first `for` loop). `for i in t: a=i e=a.replace("'","").replace('"','').split(":") if e[0] == 'age': # <- here e[1] = int(e[1]) w.append(e)` . sorry the question is closed :) – Avishka Dambawinna Jan 22 '22 at 12:38

2 Answers2

1

Given this string

string = '{"name":"John", "age":30, "city":"New York"}'

Parse it to a dict with

import ast
data = ast.literal_eval(string)

then get its items

print(list(data.items()))

[('name', 'John'), ('age', 30), ('city', 'New York')]
khelwood
  • 55,782
  • 14
  • 81
  • 108
  • isnt there basic ways without all this import staff and libr? – Aha Jan 22 '22 at 12:14
  • It doesn't get any more basic than this. `ast` is module from Pythons standard library and if it provides you with a simple solution you should use it and not try to invent the wheel yourself. – Matthias Jan 22 '22 at 13:01
1

This is JSON

import json

spam = '{"name":"John", "age":30, "city":"New York"}'
eggs = json.loads(spam)
print(eggs)
print(eggs['age'])

output:

{'name': 'John', 'age': 30, 'city': 'New York'}
30
buran
  • 13,682
  • 10
  • 36
  • 61
  • i kinda know that with json but in this case Its USER INPUT {"name":"John", "age":30, "city":"New York"} – Aha Jan 22 '22 at 12:15
  • So, store the user input in a variable and use it. What is the problem? I just hard-code the user input for the code example. – buran Jan 22 '22 at 12:16
  • i think u right but i just want to be able to do it without some imports – Aha Jan 22 '22 at 12:19