Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
nested dictionaries to CSV
#4
Hi all,

I did a small check against one key only to try finding out the reason for the mismatch in data when parsing the dictionary to the csv:

import csv

fields = ['CHK']
a = {'LCDY_PS-OT_PSC_46.90_-6.5_0.0': {'DIR': 'C:/Users/Usr/Desktop/Test/Test 1', 'FILE': 'PS-OT;R300;PSC;WD47;ROT-6.5;SEA2.0-6.0;DIR000-180.ofw', 'ID': 'DY', 'VSL': 'PS-OT', 'RAD': '300', 'PROP': 'PSC', 'WD': '47', 'ROT': '-6.5', 'HS': '2.0', 'TZ': '6.0', 'WEA': [0.0], 'NTT': '1200.00', 'NBT': '844.10', 'MX_OBS': '.2023', 'MX_SBS': '.2031', 'MN_VTS': 4.06, 'MX_VTS': 5.51, 'AST': '51.52', 'LB_OBA': '0.645', 'LB_OBB': '0.606', 'LB_SBA': '0.547', 'LB_SBB': '0.547', 'XTDP': '289.90', 'FDR': '0.017', 'FST': '9.25', 'FPL': '24.4', 'FATP': '5.04856863478451E-0005', 'FATC': '1.27143382878945E-0004', 'NO_OBS': '.1988', 'NO_SBS': '.1988', 'NO_VTS': 4.7, 'VTL': 0.0, 'HTL': 0.0, 'CHK': None}}


with open("C:\\Users\\MBo\\Desktop\\test_output.csv", "w") as f:
    w = csv.DictWriter(f, fields)
    w.writeheader()
    for k in a:
        w.writerow({field: a[k].get(field) or k for field in fields})
This returns:
Output:
CHK LCDY_PS-OT_PSC_46.90_-6.5_0.0
Which I do not understand. Why is it returning this when it should return "None".
If I check it against:

fields = ['VSL']  
It returns:
Output:
VSL PS-OT
Reply


Messages In This Thread
nested dictionaries to CSV - by mart79 - Jul-23-2019, 01:17 PM
RE: nested dictionaries to CSV - by ichabod801 - Jul-23-2019, 01:31 PM
RE: nested dictionaries to CSV - by mart79 - Jul-25-2019, 08:30 AM
RE: nested dictionaries to CSV - by mart79 - Jul-25-2019, 11:16 AM
RE: nested dictionaries to CSV - by ichabod801 - Jul-25-2019, 11:50 AM
RE: nested dictionaries to CSV - by mart79 - Jul-25-2019, 12:35 PM
RE: nested dictionaries to CSV - by ichabod801 - Jul-25-2019, 05:28 PM
RE: nested dictionaries to CSV - by mart79 - Jul-26-2019, 10:33 AM
RE: nested dictionaries to CSV - by ichabod801 - Jul-26-2019, 12:14 PM
RE: nested dictionaries to CSV - by mart79 - Jul-29-2019, 04:59 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Searching through Nested Dictionaries and Lists Dave_London 1 8,636 Jul-09-2020, 03:36 PM
Last Post: mrdominikku
  Creating Nested Dictionaries Confusion gw1500se 2 2,186 May-18-2020, 11:16 PM
Last Post: gw1500se
  Finding value in nested dictionaries with lists mart79 16 8,376 Mar-08-2020, 08:16 PM
Last Post: ndc85430
  Transform simplified dictionary to nested dictionaries bhojendra 1 2,398 Jul-02-2019, 02:05 PM
Last Post: ichabod801
  Help to flatten list of nested dictionaries shawbapmp 4 5,774 Feb-25-2019, 10:18 PM
Last Post: shawbapmp
  Nested Dictionaries with Lists BriHaug 1 56,027 Feb-23-2019, 02:10 AM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020