My data.json is
{"a":[{"b":{"c":{ "foo1":1, "foo2":2, "foo3":3, "foo4":4}}}],"d":[{"e":{"bar1":1, "bar2":2, "bar3":3, "bar4":4}}]}
I am able to list both key/pair values. My code is:
#! /usr/bin/python
import json
from pprint import pprint
with open('data2.json') as data_file:
data = json.load(data_file)
pprint(data["d"][0]["e"])
Which gives me:
{u'bar1': 1, u'bar2': 2, u'bar3': 3, u'bar4': 4}
But I want to display only the keys without any quotes and u like this:
bar1, bar2, bar3, bar4
Can anybody suggest anything? It need not be only in python, can be in shell script also.
The keys of this object are instances of the unicode string class. Given this, the default printing behavior of the dict instance for which they are the keys will print them as you show in your post.
This is because the dict implementation of representing its contents as a string (__repr__ and/or __str__) seeks to show you what objects reside in the dict, not what the string representation of those objects looks like. This is an important distinction, for example:
In [86]: print u'hi'
hi
In [87]: x = u'hi'
In [88]: x
Out[88]: u'hi'
In [89]: print x
hi
This should work for you, assuming that printing the keys together as a comma-separated unicode is fine:
print ", ".join(data["d"][0]["e"])
You can achieve this using the keys member function from dict too, but it's not strictly necessary.
print ', '.join((data["d"][0]["e"].keys()))
data["d"][0]["e"] returns a dict. In python2, You could use this to get the keys of that dict with something like this:
k = data["d"][0]["e"].keys()
print(", ".join(k))
In python3, wrap k in a list like this
k = list(data["d"][0]["e"].keys())
print(", ".join(k))
Even simpler, join will iterate over the keys of the dict.
print(", ".join(data["d"][0]["e"]))
Thanks to #thefourtheye for pointing this out.
Related
I am new to python and I am struggling with encoding
I have a list of String like this:
keys = ["u'part-00000-6edc0ee4-de74-4f82-9f8c-b4c965896224-c000.csv'",
" u'part-00001-6edc0ee4-de74-4f82-9f8c-b4c965896224-c000.csv'"]
I do this to encode
keys = [x.encode('UTF-8') for x in keys]
However I am getting "b" appended, the result being
[b"u'part-00000-6edc0ee4-de74-4f82-9f8c-b4c965896224-c000.csv'",
b" u'part-00001-6edc0ee4-de74-4f82-9f8c-b4c965896224-c000.csv'"]
I thought it would be simpler to just encode with utf-8
What am I doing wrong?
You should first try fixing the method you use to obtain your original list of strings, but if you have no control on that, you can use the following:
>>> import ast
>>> [ast.literal_eval(i.strip()) for i in keys]
The result should be
[u'part-00000-6edc0ee4-de74-4f82-9f8c-b4c965896224-c000.csv',
u'part-00001-6edc0ee4-de74-4f82-9f8c-b4c965896224-c000.csv']
for Python 2, and
['part-00000-6edc0ee4-de74-4f82-9f8c-b4c965896224-c000.csv',
'part-00001-6edc0ee4-de74-4f82-9f8c-b4c965896224-c000.csv']
for Python 3.
I am attempting to generate a URL link in the following format using urllib and urlencode.
<img src=page.psp?KEY=%28SpecA%2CSpecB%29&VALUE=1&KEY=%28SpecA%2C%28SpecB%2CSpecC%29%29&VALUE=2>
I'm trying to use data from my dictionary to input into the urllib.urlencode() function however, I need to get it into a format where the keys and values have a variable name, like below. So the keys from my dictionary will = NODE and values will = VALUE.
wanted = urllib.urlencode( [("KEY",v1),("VALUE",v2)] )
req.write( "<a href=page.psp?%s>" % (s) );
The problem I am having is that I want the URL as above and instead I am getting what is below, rather than KEY=(SpecA,SpecB) NODE=1, KEY=(SpecA,SpecB,SpecC) NODE=2 which is what I want.
KEY=%28SpecA%2CSpecB%29%2C%28%28SpecA%2CSpecB%29%2CSpecC%29&VALUE=1%2C2
So far I have extracted keys and values from the dictionary, extracted into tuples, lists, strings and also tried dict.items() but it hasn't helped much as I still can't get it to go into the format I want. Also I am doing this using Python server pages which is why I keep having to print things as a string due to constant string errors. This is part of what I have so far:
k = (str(dict))
ver1 = dict.keys()
ver2 = dict.values()
new = urllib.urlencode(function)
f = urllib.urlopen("page.psp?%s" % new)
I am wondering what I need to change in terms of extracting values from the dictionary/converting them to different formats in order to get the output I want? Any help would be appreciated and I can add more of my code (as messy as it has become) if need be. Thanks.
This should give you the format you want:
data = {
'(SpecA,SpecB)': 1,
'(SpecA,SpecB,SpecC)': 2,
}
params = []
for k,v in data.iteritems():
params.append(('KEY', k))
params.append(('VALUE', v))
new = urllib.urlencode(params)
Note that the KEY/VALUE pairings may not be the order you want, given that dicts are unordered.
I have the dictionary of items from which i am generating the URLs like this
request.build_absolute_uri("/myurl/" + urlencode(myparams))
The output i am getting is like this
number=['543543']®ion=['5,36,37']
but i want the url to be
number=543543®ion=5,36,37
all those items are in myparams dictionary
You'll probably find for ease of use that passing doseq=True will be useful (albeit not exactly what you want - but does mean that any url parsing library should be able to handle the input without custom coding...)
>>> from urllib import urlencode
>>> a = range(3)
>>> urlencode({'test': a})
'test=%5B0%2C+1%2C+2%5D'
>>> urlencode({'test': a}, True)
'test=0&test=1&test=2'
Otherwise, you'll have to write custom code to ','.join(str(el) for el in your_list) for values in myparams where it's a list/similar... (then .split(',') the other end)
looks like myparams is a dict that has lists as values.
new_params = dict(k, v[0] for k, v in dict.iteritems())
will construct a new dict.
I know its kind of trivial thing to ask...but i'm a new bee to python.
Here is a json string
reply = {u'f': [{u'v': u'0'}]}
How to parse out value 0 from it using python.
i tried like
count = reply['rows'][0]['v']
but it not working
count = reply['f'][0]['v'] should work I believe.
reply is a dictionary. As such, you need to use the dictionary keys to access the data. In this case, the key is 'f', not 'rows'.
If you had valid JSON, you could use the simplejson module:
from simplejson import loads, dumps
my_dict = loads(my_json_serialized_string)
and then you can access the python dict, e.g.,:
print my_dict.items()
print my_dict.keys()
print my_dict.values()
#lets assume 'rows' exists as a key, and the value is a list, and the first item of that list is a dict that contains the key 'v':
print my_dict['rows'][0]['v']
and you can even change the dict, and serialize it as a valid JSON string:
my_dict['my_key'] = 'my_value'
my_other_json_serialized_string = dumps(my_dict)
I have a dictionary, containing several hundred entries, of format:
>>>dict
{'1620': 'aaaaaa'}
I would like to make new empty lists named '1620', etc. I have tried variations of the following but it doesn't recognize eachkey as a variable to be used when creating the list. Instead, it names the list literally "eachkey" and my key, in this example '1620', is not connected to the new list.
>>>for eachkey in dict.keys():
>>> eachkey=[]
>>>
>>>eachkey
[]
>>>'1620'
1620
Edited to add:
Maybe I could make the list at the same time as I make the dictionary? Slip it in here below? The str(eachfile[-4:]) is what I want the list named.
files=open(sys.argv[1])
dict={}
for eachfile in files:
value=open(eachfile)
key=str(eachfile[-4:])
dict[key]=value
eachfile.close()
Edit: it would be fine for me to add letters along w/ the numbers if that's what it needs.
I don't think it's possible to change the integer literal 1620 so that it gives you an object other than the integer 1620. Similarly I don't think you can change the string literal '1620' to give you a list instead of a string.
You could do it if you prefix the variable names with some letters to make them valid names. For example you could use my1620 instead of 1620. I wouldn't advise doing this, but it's possible:
>>> d = {'1620': 'aaaaaa'}
>>> for k,v in d.items():
... locals()['my'+k] = []
>>> my1620
'aaaaaa'
With a dict like this:
d1 = {'foo':'bar', '1621':'hello'}
Try doing this:
d2 = dict((k,list()) for k in d1.keys())
Now d2 is:
{'1621': [], 'foo': []}
And you can reference your lists list so:
d2['1621'].append(20)
d2['foo'].append(5)
d2['foo'].append('zig')
Which makes d2:
{'1621': [20], 'foo': [5, 'zig']}
As Gareth said, it's VERY unlikely you really want to do what you're asking to do. This is probably better.