Jul-08-2020, 07:25 PM
As a python beginner, I like to take a CSV file and load and then output json
I have tried a few things - pandas, csvmapper but I'm struggling to see the best way to to read this and create the sample json from the CSV. I think it is a Dictionary with 2 lists embedded but obvious how to approach this
I would like to create this
https://medium.com/coinmonks/parsing-a-s...18f5c70bd3
I have tried a few things - pandas, csvmapper but I'm struggling to see the best way to to read this and create the sample json from the CSV. I think it is a Dictionary with 2 lists embedded but obvious how to approach this
Quote:A,B,source,status,RecordType,xA,xB
q1,,Direct,New, Quote,x200,,
x1,y2,Direct,New, Quote,x200,y300
I would like to create this
[{ "RecIds": [{ "type": "A", "value": "q1" }], "source": "Direct", "status": "New", "recordType": "Quote", "xIds": [{ "type": "B", "value": "x200" }] }, { "RecIds": [{ "type": "A", "value": "x1" }, { "type": "B", "value": "y2" }], "source": "Direct", "status": "New", "recordType": "Quote", "xIds": [{ "type": "A", "value": "x200" }, { "type": "B", "value": "y200" }] }]Best approach that I have found is so thing like this
https://medium.com/coinmonks/parsing-a-s...18f5c70bd3
import csv, json, xlrd def csvParse(csvfile): # Open the CSV f = open(csvfile, 'r') # Change each fieldname to the appropriate field name. reader = csv.DictReader(f, fieldnames=( "Protocol", "Source Port", "Destination Port", "Source IP", "Destination IP", "Source MAC", "Destination MAC", "segment ID", "Frame name", "Frame ID", "segment name", "packet ID", "Receivers")) framenames = [] store = [] # Store frame names in a list for row in reader: frame = {"FrameName": row["Frame name"], "FrameID": row["Frame ID"], "protocol": row["Protocol"], "segments": []} if row["Frame name"] not in framenames: framenames.append(row["Frame name"]) store.append(frame) # Create Objects for Frames, segments and packets segment = {"segmentName": ""} for frame in store: f = open(csvfile, 'r') reader = csv.DictReader(f, fieldnames=( "Protocol", "Source Port", "Destination Port", "Source IP", "Destination IP", "Source MAC", "Destination MAC", "segment ID", "Frame name", "Frame ID", "segment name", "packet ID", "Receivers")) for row in reader: if frame["FrameName"] == row["Frame name"]: if segment["segmentName"] != row["segment name"]: segment = { "segmentID": row["segment ID"], "segmentName": row["segment name"], "srcPort": row["Source Port"], "destPort": row["Destination Port"], "packets": [{ "packetID": row["packet ID"], "Receivers": row["Receivers"], "destIP": row["Destination IP"], "destMAC": row["Destination MAC"], "srcIP": row["Source IP"], "srcMAC": row["Source MAC"] }] } frame["segments"].append(segment) else: packet = { "packetID": row["packet ID"], "Receivers": row["Receivers"], "destIP": row["Destination IP"], "destMAC": row["Destination MAC"], "srcIP": row["Source IP"], "srcMAC": row["Source MAC"] } segment["packets"].append(packet) # Parse the CSV into JSON out = json.dumps(store, indent=4) # Save the JSON f = open('data.json', 'w') f.write(out)But as I said not sure if this is the right or best approach