Home > Back-end >  Dict columns extraction using for loop
Dict columns extraction using for loop

Time:01-03

There is a dict named 'data'

{
   "error":false,
   "message":"Ok",
   "data":{
      "numFound":1845,
      "start":0,
      "numFoundExact":true,
      "docs":[
         {
            "sub_farmer_id":0,
            "grade_a_produce":320,
            "commodity_image":"red_carrot.jpg",
            "farm_image":"",
            "batch_status":"completed",
            "batch_count":30,
            "franchise_type":"TELF",
            "sr_assignee_id":0,
            "farm_status":"converted",
            "state_name":"RAJASTHAN",
            "farmer_id":1648,
            "grade_a_sell_price":0,
            "id":"11",
            "commodity_name":"Carrot Red",
            "acerage":1,
            "soil_k":0,
            "lgd_state_id":8,
            "soil_n":0,
            "unique_key":"11_8",
            "historic_gdd":0.41,
            "farm_type":"soiless",
            "soil_test_report":"",
            "user_id":1648,
            "expected_yield_delivery_date":"2020-04-30T00:00:00Z",
            "current_gdd":0,
            "soil_p":0,
            "expected_grade_a_produce":636,
            "water_ph":0,
            "end_date":"2020-04-30T00:00:00Z",
            "batch_id":8,
            "expected_delivery_date":"2020-04-30T00:00:00Z",
            "previous_crop_ids":"0",
            "batch_updated_at":"2021-12-29T17:50:58Z",
            "grade_c_rejection":0,
            "water_test_report":"",
            "farm_updated_at":"2021-12-29T17:51:00Z",
            "water_ec":0,
            "start_date":"2019-10-30T00:00:00Z",
            "assignee_id":0,
            "pest_problems":"",
            "expected_production":1060,
            "is_active":true,
            "mobile":"7015150636",
            "grade_b_sell_price":0,
            "irrigation_type":"drip",
            "total_acerage":0,
            "current_pdd":0,
            "commodity_id":68,
            "stage":"flowering",
            "farm_health":"0",
            "expected_grade_b_produce":424,
            "grade_b_produce":740,
            "historic_pdd":0.31,
            "username":"Agritecture",
            "variety_name":"",
            "sr_assignee_name":"",
            "lng":0,
            "locality":"",
            "assignee_name":"",
            "subfarmer_mobile_no":"",
            "commodity_image_96px_icon":"",
            "subfarmer_name":"",
            "batch_end_date":"",
            "lat":0,
            "_version_":1720553030989906000
         }

But I am trying to extract data from list and append in a new csv with different columns so that it looks neat and clean

so, Here I am trying code

       writer = csv.DictWriter(response_data, fieldnames=['Farmer Name', 'Mobile', 'Irrigation Type', 'Batch Status',
                                                           'Soil Parameters',
                                                           'Water Parameters', 'Crop Name', 'Farm Status', 'Farm Type',
                                                           'Franchise Type', 'Farm Total Acerage', 'Batch Acerage',
                                                           'Farm Health(%)', 'Historical Yield(/Acre)',
                                                           'Expected Produce',
                                                           'Actual Yield(/Acre)', 'Actual Produce', 'Crop health(%)',
                                                           'Stage', 'SOP Adherence', 'Assignee', 'Sub Farmer',
                                                           'Last Activity Update', 'Exp. Delivery Date'], delimiter=",")
        writer.writeheader()

        for docs in data.keys():
            writer.writerow(
                {"Farmer Name": docs.get('username'), "Mobile": docs.get('mobile')
                 "Irrigation Type": data.get('irrigation_type'), "Batch Status": data.get('batch_status'),
                 "Soil Parameters": {'N:-': data.get('soil_n'), 'P:-': data.get('soil_p'),
                                     'K:-': data.get('soil_k')},
                 "Water Parameters": {'ec:-': data.get('water_ec'), 'pH:-': data.get('water_ph'), },
                 "Crop Name": data.get('commodity_name'), "Farm Status": data.get('farm_status'),
                 "Farm Type": data.get('farm_type'),
                 "Franchise Type": data.get('franchise_type'), "Farm Total Acerage": data.get('total_acerage'),
                 "Batch Acerage": data.get('batch_count'), "Farm Health(%)": data.get('farm_health'),
                 "Historical Yield(/Acre)": data.get(''),
                 "Expected Produce": data.get('expected_production'), "Actual Yield(/Acre)": data.get('username'),
                 "Actual Produce": data.get('username'), "Crop health(%)": data.get('username'),
                 "Stage": data.get('stage'),
                 "SOP Adherence": data.get('sope'),
                 "Last Activity Update": data.get('end_date'),
                 "Exp. Delivery Date": data.get('expected_delivery_date')
                 })

SO that the data extract from that dict and keep on append on the cdv file as methioned column

But It shows an error

AttributeError: 'str' object has no attribute 'get'

I could not get the columns value in my header from the dict

CodePudding user response:

Firstly, it is confusing that you have a dict called data which has a key called "data", and that you are iterating over docs in a dict with a key called "docs".

However, the error is because you are iterating over data.keys(), which returns a string for each value.

An illustrative example:

my_dict = {
    "foo" : "bar",
    "baz" : "luhrmann"
}

for each_key in my_dict.keys():
    print(f"{each_key} : {type(each_key)}")

This will return:

foo : <class 'str'>
baz : <class 'str'>

So in the line docs.get('username'), you are trying to call the get method on a string. The error is telling you that strings do not have this method. I think what you are trying to do is data['docs'].get('username').

CodePudding user response:

The full code:

import csv

with open('output.csv', 'w') as response_data:
    writer = csv.DictWriter(response_data, fieldnames=['Farmer Name', 'Mobile', 'Irrigation Type', 'Batch Status',
                                                       'Soil Parameters', 
                                                       'Water Parameters', 'Crop Name', 'Farm Status', 'Farm Type',
                                                       'Franchise Type', 'Farm Total Acerage', 'Batch Acerage',
                                                       'Farm Health(%)', 'Historical Yield(/Acre)',
                                                       'Expected Produce',
                                                       'Actual Yield(/Acre)', 'Actual Produce', 'Crop health(%)',
                                                       'Stage', 'SOP Adherence', 'Assignee', 'Sub Farmer',
                                                       'Last Activity Update', 'Exp. Delivery Date'], delimiter=",")
    writer.writeheader()

    for doc in data['data']['docs']:  # THE PROBLEM WAS HERE
        writer.writerow({
            "Farmer Name": doc.get('username'), "Mobile": doc.get('mobile'),
            "Irrigation Type": doc.get('irrigation_type'), "Batch Status": doc.get('batch_status'),
            "Soil Parameters": {'N:-': doc.get('soil_n'), 'P:-': doc.get('soil_p'),
                               'K:-': doc.get('soil_k')},
            "Water Parameters": {'ec:-': doc.get('water_ec'), 'pH:-': doc.get('water_ph'), },
            "Crop Name": doc.get('commodity_name'), "Farm Status": doc.get('farm_status'),
            "Farm Type": doc.get('farm_type'),
            "Franchise Type": doc.get('franchise_type'), "Farm Total Acerage": doc.get('total_acerage'),
            "Batch Acerage": doc.get('batch_count'), "Farm Health(%)": doc.get('farm_health'),
            "Historical Yield(/Acre)": doc.get(''),
            "Expected Produce": doc.get('expected_production'), "Actual Yield(/Acre)": doc.get('username'),
            "Actual Produce": doc.get('username'), "Crop health(%)": doc.get('username'),
            "Stage": doc.get('stage'),
            "SOP Adherence": doc.get('sope'),
            "Last Activity Update": doc.get('end_date'),
            "Exp. Delivery Date": doc.get('expected_delivery_date')
        })

output.csv:

Farmer Name,Mobile,Irrigation Type,Batch Status,Soil Parameters,Water Parameters,Crop Name,Farm Status,Farm Type,Franchise Type,Farm Total Acerage,Batch Acerage,Farm Health(%),Historical Yield(/Acre),Expected Produce,Actual Yield(/Acre),Actual Produce,Crop health(%),Stage,SOP Adherence,Assignee,Sub Farmer,Last Activity Update,Exp. Delivery Date
Agritecture,7015150636,drip,completed,"{'N:-': 0, 'P:-': 0, 'K:-': 0}","{'ec:-': 0, 'pH:-': 0}",Carrot Red,converted,soiless,TELF,0,30,0,,1060,Agritecture,Agritecture,Agritecture,flowering,,,,2020-04-30T00:00:00Z,2020-04-30T00:00:00Z

How I build data:

import json
data = json.loads(your_json_data)
print(data)

# Output
{'error': False,
 'message': 'Ok',
 'data': {'numFound': 1845,
  'start': 0,
  'numFoundExact': True,
  'docs': [{'sub_farmer_id': 0,
    'grade_a_produce': 320,
    'commodity_image': 'red_carrot.jpg',
    'farm_image': '',
    'batch_status': 'completed',
    'batch_count': 30,
    'franchise_type': 'TELF',
    'sr_assignee_id': 0,
    'farm_status': 'converted',
    'state_name': 'RAJASTHAN',
    'farmer_id': 1648,
    'grade_a_sell_price': 0,
    'id': '11',
    'commodity_name': 'Carrot Red',
    'acerage': 1,
    'soil_k': 0,
    'lgd_state_id': 8,
    'soil_n': 0,
    'unique_key': '11_8',
    'historic_gdd': 0.41,
    'farm_type': 'soiless',
    'soil_test_report': '',
    'user_id': 1648,
    'expected_yield_delivery_date': '2020-04-30T00:00:00Z',
    'current_gdd': 0,
    'soil_p': 0,
    'expected_grade_a_produce': 636,
    'water_ph': 0,
    'end_date': '2020-04-30T00:00:00Z',
    'batch_id': 8,
    'expected_delivery_date': '2020-04-30T00:00:00Z',
    'previous_crop_ids': '0',
    'batch_updated_at': '2021-12-29T17:50:58Z',
    'grade_c_rejection': 0,
    'water_test_report': '',
    'farm_updated_at': '2021-12-29T17:51:00Z',
    'water_ec': 0,
    'start_date': '2019-10-30T00:00:00Z',
    'assignee_id': 0,
    'pest_problems': '',
    'expected_production': 1060,
    'is_active': True,
    'mobile': '7015150636',
    'grade_b_sell_price': 0,
    'irrigation_type': 'drip',
    'total_acerage': 0,
    'current_pdd': 0,
    'commodity_id': 68,
    'stage': 'flowering',
    'farm_health': '0',
    'expected_grade_b_produce': 424,
    'grade_b_produce': 740,
    'historic_pdd': 0.31,
    'username': 'Agritecture',
    'variety_name': '',
    'sr_assignee_name': '',
    'lng': 0,
    'locality': '',
    'assignee_name': '',
    'subfarmer_mobile_no': '',
    'commodity_image_96px_icon': '',
    'subfarmer_name': '',
    'batch_end_date': '',
    'lat': 0,
    '_version_': 1720553030989906000}]}}
  • Related