I have the dict of lists that stored in database as a text
a = {u'1': [u'12'], u'2': [u'7', u'8', u'9']}
I want manipulating with this structure as a dict of lists.
a["2"][3] = 9
but haven't any idea how to convert this string to dict of lists back.
You should store that data as JSON rather than just the repr of a dict. Then you can easily convert to and from that format using the json library.
Tnx. I can do what I want using JSON
import json
a = {u'1': [u'12'], u'2': [u'7', u'8', u'9']}
Make JSON
y = json.dumps(a)
b = json.loads(y)
And finally we have
>>> b["2"][1]
8
Related
I would like to insert a json object into a Hbase cellusing scala, presently i'm able to insert values using the below code but would like to know how i may be able to insert the entire Json object into a Hbase cell.
import org.apache.hadoop.hbase.util.Bytes.toBytes
val hTable:HTable = new HTable(configuration, "tablename")
val p = new Put(Bytes.toBytes("row1"))
p.add(Bytes.toBytes("info"),Bytes.toBytes("firstname)",Bytes.toBytes("Jim"))
hTable.put(p)
hTable.close()
You can encode your json object as a string. then encode this string as byte array. then put this byte array in Hbase. pseudo code will be like this:
json = createYourJson()
jsonString = json.toString
jsonBytyes = Bytes.toBytes(jsonString)
put.add(yourColumnFamily, yourQualifier, jsonBytes)
and when loading the value from hbase you have to reverse this order. Pseudo code will be like this:
jsonBytes = hbase.get(table, columnFamily, qualifier)
jsonString = Bytes.toString(jsonBytes)
json = Json.parse(jsonString)
Datatables is sending to Django the following query string parameters:
action:remove
data[1][DT_RowId]:1
data[1][volume]:5.0
data[1][coeff]:35
data[2][DT_RowId]:2
data[2][volume]:4.0
data[2][coeff]:50
I can access the values like this:
print request.GET['data[1][volume]']
5.0
How can I access the key itself as a dictionary and its keys?
For example, I would like to access the value as data[1]['volume']. In addition, I need to access the keys; e.g. get 1 from data[1].
I think you will need to parse the keys, yourselves and convert them to dictionary. This could be done quickly using the regular expression module in python.
import re
pattern = re.compile("data\[(?P<key_one>.*?)\]\[(?P<key_two>.*?)\]")
match = pattern.match('data[1][volume]')
key_one = match.group('key_one')
key_two = match.group('key_two')
print(key_one) # Should print 1
print(key_two) # Should print volume
See Python documentation of its regular expression library to learn more.
I have a list of ordered tuples which each tuple contains column name and value pair to be written to a csv for example
lst = [('name','bob'),('age',19),('loc','LA')]
which has in for for bob, age 19 and location, loc, in LA. I want to be able to write this to CSV file based on column names and sometimes some of these columns are missing, for example for another row.
lst2 = [('name','bob'),('loc','LA')]
age is missing, how I can write these rows properly in python to a csv?
Those tuples can be used to initialize a dict so csv.DictWriter seems the best choice. In this example I create a dict filled with default values. For each list of tuples, I copy the dict, update with the known values and write it out.
import csv
# sample data
lst = [('name','bob'),('age',19),('loc','LA')]
lst2 = [('name','jane'),('loc','LA')]
lists = [lst, lst2]
# columns need some sort of default... I just guessed
defaults = {'name':'', 'age':-1, 'loc':'N/A'}
with open('output.csv', 'wb') as outfile:
writer = csv.DictWriter(outfile, fieldnames=sorted(defaults.keys()))
writer.writeheader()
for row_tuples in lists:
# copy defaults then update with known values
kv = defaults.copy()
kv.update(row_tuples)
writer.writerow(kv)
# debug...
print open('output.csv').read()
You should give more examples, as to what exactly is required- as what if the location is not given in ls2 then what do you want to write to your csv? From what I understand, you can make a function and default argument:
import csv
def write_tuples_to_csv(name="DefaultName", age="DefaultAge", loc="Default location"):
writer = csv.writer(open("/path/to/csv/file", 'a')) # appending to a file
row = (name, age, loc)
writer.writerow(['name','num','location'])
writer.writerow(row)
Now you can call this function for every item in the list. This should help you to get you started.
I'm trying to convert an RDD that has a fixed size lists of strings (a result of parsing CSV file) into and RDD of Rows. This is so I can turn it into a dataframe, because I need it into a dataframe to write to parquet. Anyway the only part I need help with is the converting of Rdd from list of strings to Row.
The RDD variable name is RDD
I used:
import org.apache.spark.sql._
val RowRDD = RDD.map(r => Row.fromSeq(r))
I'm trying to use numbers as my dict key. Is there anyway to initiate the dictionary using dict() method?
This works
mydict = { '100':'hundred', '200':'two hundred'}
This doesn't work?
mydict = dict( 100='hundred' )
The error says 'keyword can't be an expression' and I couldn't find any solution.
Thank you.
I can't understand your question exactly, but you mentioned to use number as dict key right? you just directly initiate it using integer instead string like this..
a = {1:'one',100:'hundered'}
print a
{1: 'one', 100: 'hundrered'}
No, it mist be a valid python identifier, so it cannot start with a number.
You can read where i found it at here in the part about dict
https://docs.python.org/2/library/stdtypes.html#typesmapping
Like the comment above says you can use an int, since dictionaries just hash the string and you get an int anyways, in the case of an int it just hashes to itself. But it doesnt work with dict ()
On that page it shows you can do
mydict = dict (zip ([1], ["one"]))
Which is kinda ugly imo, but seems to get the job done
To use the dict method you need to feed it a list or tuple of lists or tuples.
>>> dict([(100, 'hundred'), (200, 'two hundred')])
{200: 'two hundred', 100: 'hundred'}