Question: I need help with a python CSV to JSON filter, thank you INPUT: Consider the following input files: vessel raw data.txt e contains vessel records
I need help with a python CSV to JSON filter, thank you



INPUT: Consider the following input files: vessel raw data.txt e contains vessel records if the form IMO number 9500041;MMSI|373138000;Name of the ship CENTENARIO FORZA; Former names | AENTEN (2017, Panama);Vessel type Bulk carrier; Operating status Active; Flag Panama; Gross tonnage|31754 tons;Deadwei ght|56129 tons; Length|189 m; Breadth|32 m;Year of build|2012; Builder|MITSUI ICHIHARA ENGINEERING & SHIPBUILDING - ICHIHARA, JAPAN;Classification society NIPPON KAIJI KYOKAI (NKK); Owner MK CENTENNIAL MARITIME - AMSTERDAM, NETHERLANDS; Manager |MAYBARU SHIPPING & TRADING - SINGAPORE; Description CENTENARIO FORZA is a Bulk carrier built in 2012 by MITSUI ICHIHARA ENGINEERING & SHIPBUILDING - ICHIHAR A, JAPAN. Currently sailing under the flag of Panama. Formerly also known as AENTEN. It's gross tonnage is 31754 tons.;Seafarers worked on No sea service records found . imo to callsign and type.txt 2 contains a mapping of IMO numbers to call signs and vessel type, of the form IMO Callsign VesselType 5898610 WCY5117 Pusher/Tug 5683467 RAI Pusher/Tug 5557230 B9037 Fishing vessel 5481900 VORD Pusher/Tug 5429201 D2D8G2 Pusher/Tug 5429081 ERAH Special Purpose 5428817 XPMB Fishing vessel 5428702 E502579 Pusher/Tug 5428439 PPPS Special Purpose OUTPUT: The goal is to cross the data contained in both files, in order to send on the standard output (console) a list of vessels, with the following features: 1. The last two fields ('Description' and 'Seafarers worked on') of the first file are discarded 2. A 'Call Sign' field is added to a vessel record, only if the call sign matching the vessel's IMO can be found in the mapping. 3. Each vessel record should be encoded as a JSON string, one per line For example: {"Call Sign": "3EXU5", "IMO number": "9500041", "MMSI": "373138000", "Name of the ship": "CENTENARIO FORZA", "Former names": "AENTEN (2017, Panama)%", "Vessel type": "Bulk carrier", "Operating stat us": "Active", "Flag": "Panama", "Gross tonnage": "31754 tons", "Deadweight": "56129 tons", "Length": "189 m", "Breadth": "32 m", "Year of build": "2012", "Builder": "MITSUI ICHIHARA ENGINEERING & SHIPBUILDING - ICHIHARA, JAPAN", "Classification society": "NIPPON KAIJI KYOKAI (NKK)", "Owner": "MK CENTENNIAL MARITIME - AMSTERDAM, NETHERLANDS", "Manager": "MAYBARU SHIPPING & TRADING - SINGAPOR INTERFACE: Your Python script is to be named cross_imo_and_callsign.py reads to the list of vessels from the standard input reads the imo/callsign mapping from the file passed as an argument Accordingly, it is to be called as follows: cat vessel_raw_data.txt | python3 cross_imo_and_callsign.py imo_to_callsign_and_type.txt . The sequence of operations is roughly this one: create an empty hash table dict() type, or ':') open the imo/callsign mapping file loop over the file: from each line, create a new entry in the hash table, that has the IMO number for a key, and the call sign for a value when done, start another loop, that reads the standard input for each line read o create a new vessel object, i.e. an empty dictionary (1) o split the line, using the semi-colon (;') as a separator . for each resulting field, split again, using the pipe (T) as a separator: the LHS token is the field name, the RHS token is the field value; if the field is the IMO number, check for a call sign in the imo/callsign dictionary, and create a key/value pair for the callsign in the vessel dictionary; for all other fields (excepted the last two: see requirements above), store the resulting key/value pair into the vessel dictionary o use the json.dumps() function to create a JSON string from your vessel object, and print it on the console INPUT: Consider the following input files: vessel raw data.txt e contains vessel records if the form IMO number 9500041;MMSI|373138000;Name of the ship CENTENARIO FORZA; Former names | AENTEN (2017, Panama);Vessel type Bulk carrier; Operating status Active; Flag Panama; Gross tonnage|31754 tons;Deadwei ght|56129 tons; Length|189 m; Breadth|32 m;Year of build|2012; Builder|MITSUI ICHIHARA ENGINEERING & SHIPBUILDING - ICHIHARA, JAPAN;Classification society NIPPON KAIJI KYOKAI (NKK); Owner MK CENTENNIAL MARITIME - AMSTERDAM, NETHERLANDS; Manager |MAYBARU SHIPPING & TRADING - SINGAPORE; Description CENTENARIO FORZA is a Bulk carrier built in 2012 by MITSUI ICHIHARA ENGINEERING & SHIPBUILDING - ICHIHAR A, JAPAN. Currently sailing under the flag of Panama. Formerly also known as AENTEN. It's gross tonnage is 31754 tons.;Seafarers worked on No sea service records found . imo to callsign and type.txt 2 contains a mapping of IMO numbers to call signs and vessel type, of the form IMO Callsign VesselType 5898610 WCY5117 Pusher/Tug 5683467 RAI Pusher/Tug 5557230 B9037 Fishing vessel 5481900 VORD Pusher/Tug 5429201 D2D8G2 Pusher/Tug 5429081 ERAH Special Purpose 5428817 XPMB Fishing vessel 5428702 E502579 Pusher/Tug 5428439 PPPS Special Purpose OUTPUT: The goal is to cross the data contained in both files, in order to send on the standard output (console) a list of vessels, with the following features: 1. The last two fields ('Description' and 'Seafarers worked on') of the first file are discarded 2. A 'Call Sign' field is added to a vessel record, only if the call sign matching the vessel's IMO can be found in the mapping. 3. Each vessel record should be encoded as a JSON string, one per line For example: {"Call Sign": "3EXU5", "IMO number": "9500041", "MMSI": "373138000", "Name of the ship": "CENTENARIO FORZA", "Former names": "AENTEN (2017, Panama)%", "Vessel type": "Bulk carrier", "Operating stat us": "Active", "Flag": "Panama", "Gross tonnage": "31754 tons", "Deadweight": "56129 tons", "Length": "189 m", "Breadth": "32 m", "Year of build": "2012", "Builder": "MITSUI ICHIHARA ENGINEERING & SHIPBUILDING - ICHIHARA, JAPAN", "Classification society": "NIPPON KAIJI KYOKAI (NKK)", "Owner": "MK CENTENNIAL MARITIME - AMSTERDAM, NETHERLANDS", "Manager": "MAYBARU SHIPPING & TRADING - SINGAPOR INTERFACE: Your Python script is to be named cross_imo_and_callsign.py reads to the list of vessels from the standard input reads the imo/callsign mapping from the file passed as an argument Accordingly, it is to be called as follows: cat vessel_raw_data.txt | python3 cross_imo_and_callsign.py imo_to_callsign_and_type.txt . The sequence of operations is roughly this one: create an empty hash table dict() type, or ':') open the imo/callsign mapping file loop over the file: from each line, create a new entry in the hash table, that has the IMO number for a key, and the call sign for a value when done, start another loop, that reads the standard input for each line read o create a new vessel object, i.e. an empty dictionary (1) o split the line, using the semi-colon (;') as a separator . for each resulting field, split again, using the pipe (T) as a separator: the LHS token is the field name, the RHS token is the field value; if the field is the IMO number, check for a call sign in the imo/callsign dictionary, and create a key/value pair for the callsign in the vessel dictionary; for all other fields (excepted the last two: see requirements above), store the resulting key/value pair into the vessel dictionary o use the json.dumps() function to create a JSON string from your vessel object, and print it on the console
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
