OUTPUT PHOTO
#### My work ####
from collections import OrderedDict
import json
test = {
'c': 2,
'B': '002',
'A': '{0:04d}'.format(2)
}
data = {'Author': "joe", 'data': test}
a = '{0:04d}'.format(2)
b=[1, a, '{0:04d}'.format(2)]
c = {1:a}
print (a, b, '{0:04d}'.format(2))
print(a)
print(b)
print(c)
print('{0:04d}'.format(2))
print json.dumps(data, indent=4, separators=(',', ': '))
#### Output
('0002', [1, '0002', '0002'], '0002')
0002
[1, '0002', '0002']
{1: '0002'}
0002
{
"data": {
"A": "0002",
"c": 2,
"B": "002"
},
"Author": "joe"
}
I'd like to get 0002 not "0002"
It is integer number when i use '{0:04d}'.format(2) in print().
but It is printed out as string if I put it in list or dictionary.
How can I get 4 digit number as integer type in dictionary?
Thanks
Related
I am trying to write a simple dataframe to gbq. One of the field is array so I tried using REPEATED mode. But it seems nothing is written to the array
df = pd.DataFrame(
{
'my_string': ['a', 'b', 'c'],
'my_int64': [1, 2, 3],
'my_float64': [4.0, 5.0, 6.0],
'my_timestamp': [
pd.Timestamp("1998-09-04T16:03:14"),
pd.Timestamp("2010-09-13T12:03:45"),
pd.Timestamp("2015-10-02T16:00:00")
],
'my_array': [
[1,2,3],
[4,5,6],
[7,8,9]
]
}
)
# client = bigquery.Client()
table_id = 'junk.pshah2_new_table'
# Since string columns use the "object" dtype, pass in a (partial) schema
# to ensure the correct BigQuery data type.
job_config = bigquery.LoadJobConfig(schema=[
bigquery.SchemaField("my_string", "STRING"),
bigquery.SchemaField("my_array", "INTEGER","REPEATED"),
])
job = client.load_table_from_dataframe(
df, table_id, job_config=job_config
)
# Wait for the load job to complete.
job.result()
In GBQ I do not find my_array written at all
Am I doing something wrong here?
I created a stored function in PostgreSQL returns a table, and called that function in a Django-rest-framework like this:
def getSomeFunc(self):
with connection.cursor() as cursor:
cursor.execute(f'select json_agg(myfunctionpsql) from myfunctionpsql')
table = cursor.fetchall()
return table
this function is called in a views file, like a code below:
class myview(views.APIView):
def get(self, request):
fund = getSomeFunc(self)
return Response({'data': fund}, status=status.HTTP_200_OK)
well the response is like this:
{
"data": [
[ // I want to delete this
[ // I want to delete this
{
"id": 21,
"somedata": "FIX A",
"somedata": "FIX A",
"sometag": 0.95,
"somdata": "005.119.745/0001-98",
"somedatayear": 1.57,
"somedata12": 4.11,
"somedata36": 19.58,
"somedata60": 51.9,
"datarisk": 0
}
]// I want delete this
]// I want to delete this
]
}
and I need response below:
{ "data": [{
"id": 21,
"somedata": "FIX A",
"somedata": "FIX A",
"sometag": 0.95,
"somdata": "005.119.745/0001-98",
"somedatayear": 1.57,
"somedata12": 4.11,
"somedata36": 19.58,
"somedata60": 51.9,
"datarisk": 0
}]
}
I try this:
class myview(views.APIView):
def get(self, request):
fund = getSomeFunc(self)
reps = str(fund)[1:-1]
return Response({'data': resp}, status=status.HTTP_200_OK)
but the response is all converted and return in a string, how to delete extra squared brackets, in a response.
regards.
If your variable fund is:
[
[
[
{
"id": 21,
"somedata": "FIX A",
"somedata": "FIX A",
"sometag": 0.95,
"somdata": "005.119.745/0001-98",
"somedatayear": 1.57,
"somedata12": 4.11,
"somedata36": 19.58,
"somedata60": 51.9,
"datarisk": 0
}
]
]
]
you can write a generator to flatten the items into a simple list, because I'm assuming that there could be multiple objects in your list and using indices to slice the list would be dangerous.
try this recursive generator:
def flatten_list(data):
if isinstance(data, list):
for item in data:
yield from flatten_list(item)
else:
yield data
and call it like this:
list(flatten_list(fund))
example:
data = [[[1, 4], 2], 3]
list(flatten_list(data))
# result: [1, 4, 2, 3]
I am retrieving data from multiple tables in Django.
my current response is :
{
"status": 0,
"message": "Client details retrived successfully...!!!",
"results": [
{
"id": 11,
"client_id": "CL15657917080578748000",
"client_name": "Pruthvi Katkar",
"client_pan_no": "RGBB004A11",
"client_adhar_no": "12312312313",
"legal_entity_name": "ABC",
"credit_period": "6 months",
"client_tin_no": 4564565,
"client_email_id": "abc#gmail.com",
"head_office_name": "ABC",
"office_name": "asd234",
"office_email_id": "zxc#gmail.com",
"office_contact": "022-27547119",
"gst_number": "CGST786876876",
"office_country": "India",
"office_state": "gujrat",
"office_district": "vadodara",
"office_taluka": "kachh",
"office_city": "vadodara",
"office_street": "New rode 21",
"office_pincode": 2344445,
"contact_person_name": "prasad",
"contact_person_designation": "DM",
"contact_person_number": "456754655",
"contact_person_email": "asd#gmail.com",
"contact_person_mobile": "5675545654",
"created_at": "2019-08-14T14:08:28.057Z",
"created_by": "Prathamseh",
"updated_at": "2019-08-14T14:08:28.057Z",
"updated_by": "prasad",
"is_deleted": false
},
{
"id": 11,
"user_id": "CL15657917080578748000",
"bank_details_id": "BL15657917080778611000",
"bank_name": "Pruthvi",
"branch": "vashi",
"ifsc_code": "BOI786988",
"account_number": 56756765765765,
"account_name": "Pruthvi",
"is_deleted": false
},
{
"id": 10,
"document_details_id": "DL15657917080808598000",
"user_id": "CL15657917080578748000",
"document_type": "Pruthvi ID",
"document": "www.sendgrid.com/pan",
"is_deleted": false
}
]
}
Expected Response :
I am getting the queryset form db in models.py and i am sending it to the views.py and i am iterating over the dict but not getting the expected response.
views.py
#csrf_exempt
def get_client_details(request):
try:
# Initialising lists for storing results
result = []
temp_array = []
# Getting data from request body
client_master_dict = json.loads(request.body)
# Response from get client data
records = ClientDetails.get_client_data(client_master_dict)
# Create response object
# Iterating over the records object for getting data
for i in range(len(records)):
# Converting the querysets objects to json array format
record_result_list = list(records[i].values())
# If multiple records are present
if(len(record_result_list) > 1):
for j in range(len(record_result_list)):
user_info = record_result_list[j]
temp_array.append(user_info)
result.append(temp_array)
temp_array=[]
# For single record
else:
result.append(record_result_list[0])
# Success
returnObject = {
"status" : messages.SUCCESS,
"message" : messages.CLIENT_RETRIVE_SUCCESS,
"results" : result
}
return JsonResponse(returnObject,safe=False)
I think the issue might be in my inner for loop, can anyone help me out with this, is there any way to iterate over the nested JSON object.
Models.py
#classmethod
def get_client_data(cls, client_master_dict):
try:
response_list = []
client_id = client_master_dict['client_id']
client_details = cls.objects.filter(client_id = client_id,is_deleted = False)
bank_details = BankDetails.objects.filter(user_id = client_id,is_deleted = False)
document_details = DocumentDetails.objects.filter(user_id = client_id,is_deleted = False)
response_list.append(client_details)
response_list.append(bank_details)
response_list.append(document_details)
return response_list
except(Exception) as error:
print("Error in get_client_data",error)
return False
Here i'm fetching data from 3 tables and adding it into list.
After printing the data on console i am getting :
[{'id': 11, 'client_id': 'CL15657917080578748000', 'client_name': 'Pruthvi Katkar', 'client_pan_no': 'RGBB004A11', 'client_adhar_no': '12312312313', 'legal_entity_name': 'ABC', 'credit_period': '6 months', 'client_tin_no': 4564565, 'client_email_id': 'abc#gmail.com', 'head_office_name': 'ABC', 'office_name': 'asd234', 'office_email_id': 'zxc#gmail.com', 'office_contact': '022-27547119', 'gst_number': 'CGST786876876', 'office_country': 'India', 'office_state': 'gujrat', 'office_district': 'vadodara', 'office_taluka': 'kachh', 'office_city': 'vadodara', 'office_street': 'New rode 21', 'office_pincode': 2344445, 'contact_person_name': 'prasad', 'contact_person_designation': 'DM', 'contact_person_number': '456754655', 'contact_person_email': 'asd#gmail.com', 'contact_person_mobile': '5675545654', 'created_at': datetime.datetime(2019, 8, 14, 14, 8, 28, 57874, tzinfo=<UTC>), 'created_by': 'Prathamseh', 'updated_at': datetime.datetime(2019, 8, 14, 14, 8, 28, 57874, tzinfo=<UTC>), 'updated_by': 'prasad', 'is_deleted': False}]
[{'id': 11, 'user_id': 'CL15657917080578748000', 'bank_details_id': 'BL15657917080778611000', 'bank_name': 'Pruthvi', 'branch': 'vashi', 'ifsc_code': 'BOI786988', 'account_number': 56756765765765, 'account_name': 'Pruthvi', 'is_deleted': False}]
[{'id': 10, 'document_details_id': 'DL15657917080808598000', 'user_id': 'CL15657917080578748000', 'document_type': 'Pruthvi ID', 'document': 'www.sendgrid.com/pan', 'is_deleted': False}]
Did you check the output of record_result_list? You can outright tell their if it's recovering the data in the format you requested. Try the printing to screen method to debug.
As far as I cam see, the expected output and the hierarchy of results for bank details are not matching. I don't know how you are handling the hierarchy. Are you directly taking it from JSON as the hierarchy? Or are you just taking the data and creating hierarchy in the expected output?
names=['Peter', 'John']
size = ['X', 'M', 'L']
list_price = [1, 2, 3, 4, 5, 6] # There are 2 people will buy 3 size of shirt
I want to create my data structure into:
[
{'name': u'Peter', 'size_price': defaultdict(<type 'int'>, { 'X': 1, 'M':2, 'L': 3})},
{'name': 'John', 'size_price': defaultdict(<type 'int'>, {'X':4, 'M':5, 'L':6})}
]
I prefer to do defaultdict()
You can turn list_price into an iterator and then use next to get one value after the other:
>>> iterator = iter(list_price)
>>> [{"name": n, "size_price": {s: next(iterator) for s in size}} for n in names]
[{'size_price': {'X': 1, 'M': 2, 'L': 3}, 'name': 'Peter'},
{'size_price': {'X': 4, 'M': 5, 'L': 6}, 'name': 'John'}]
Of course you do not have to use a list comprehension but can do the same thing with nested loops as well.
How to nicely convert a data.frame with hierarchical information to a JSON (or nested list)?
Let's say we have the following data.frame:
df <- data.frame(
id = c('1', '1.1', '1.1.1', '1.2'),
value = c(10, 5, 5, 5))
# id value
# 1 10
# 1.1 5
# 1.1.1 5
# 1.2 5
Then I would like to end up with the following JSON:
{
"id": "1",
"value": 10,
"children": [
{
"id": "1.1",
"value": 5,
"children": [
{
"id": "1.1.1",
"value": 5
}
]
},
{
"id": "1.2",
"value": 5
}
]
}
Where id defines the hierarchical structure, and . is a delimiter.
My intention is to easily be able to convert data from R to hierarchical D3 visualisations (e.g. Partition Layout or Zoomable Treemaps). It would also be nice if it is possible to add more "value"-columns; e.g value, size, weight, etc.
Thank you!
EDIT: I reverted to the original question, so it is easier to follow all the answers (sorry for all the editing).
I tend to have RJSONIO installed which does this:
R> df <- data.frame(id = c('1', '1.1', '1.1.1', '1.2'), value = c(10, 5, 5, 5))
R> RJSONIO::toJSON(df)
[1] "{\n \"id\": [ \"1\", \"1.1\", \"1.1.1\", \"1.2\" ],\n\"value\": [ 10, 5, 5, 5 ] \n}"
R> cat(RJSONIO::toJSON(df), "\n")
{
"id": [ "1", "1.1", "1.1.1", "1.2" ],
"value": [ 10, 5, 5, 5 ]
}
R>
That is not your desired output but the desired nesting / hierarchy was not present in the data.frame. I think if you nest a data.frame inside a list you will get there.
Edit: For your revised question, here is the R output of reading you spec'ed JSON back in:
R> RJSONIO::fromJSON("/tmp/foo.json")
$id
[1] "1"
$value
[1] 10
$children
$children[[1]]
$children[[1]]$id
[1] "1.1"
$children[[1]]$value
[1] 5
$children[[1]]$children
$children[[1]]$children[[1]]
$children[[1]]$children[[1]]$id
[1] "1.1.1"
$children[[1]]$children[[1]]$value
[1] 5
$children[[2]]
$children[[2]]$id
[1] "1.2"
$children[[2]]$value
[1] 5
R>
A possible solution.
First I define the following functions:
# Function to get the number hierarchical dimensions (occurences of "." + 1)
ch_dim <- function(x, delimiter = ".") {
x <- as.character(x)
chr.count <- function(x) length(which(unlist(strsplit(x, NULL)) == delimiter))
if (length(x) > 1) {
sapply(x, chr.count) + 1
} else {
chr.count(x) + 1
}
}
# Function to convert a hierarchical data.frame to a nested list
lst_fun <- function(ch, id_col = "id", num = min(d), stp = max(d)) {
# Convert data.frame to character
ch <- data.frame(lapply(ch, as.character), stringsAsFactors=FALSE)
# Get number of hierarchical dimensions
d <- ch_dim(ch[[id_col]])
# Convert to list
lapply(ch[d == num,][[id_col]], function(x) {
tt <- ch[grepl(sprintf("^%s.", x), ch[[id_col]]),]
current <- ch[ch[[id_col]] == x,]
if (stp != num && nrow(tt) > 0) {
c(current, list(children = lst_fun(tt, id_col, num + 1, stp)))
} else { current }
})
}
then convert the data.frame to a list:
lst <- lst_fun(df, "id")
and finally, the JSON:
s <- RJSONIO::toJSON(lst)