Monday, 11 February 2019

How to read and map CSV's multi line header rows using python

I have a CSV file which is downloaded from database(as it is in CSV) and now I have to parse into JSON Schema. Don't worry this link just github gist

enter image description here

Problem I am facing is: its Multi line Header check CSV File Here

If you take notice in the file:

  1. On 1st line of CSV it has 1st line of headers then next line has all the values for those headers.

  2. On 3rd line of CSV file it has 2nd line of headers then next line has all the values for those headers.

  3. impOn 5th line of CSV file it has 3rd line of headers then next line has all the values for those headers.

Also you can notice the pattern here,

  • 1st line of headers hasn't any tab
  • 2nd line of headers has only one tab
  • 3rd line of headers has two tabs

This goes for all the records.

Now 1st problem is this multi line of headers. And 2nd problem is how to parse it into nested json as I have. one of the solution I have tried Create nested JSON from CSV. and noticed the 1st problem with my csv.

Its been 2 days I am on this, still didn't got any kind of solution at my end.

Json Schema

{


orderRef: number,
  orderStatus: string,
  affiliate: string,
  source: string,
  agent: string,
  customer: {
    name: string,
    email: string,
    telephone: string
    mobile: string,
    address: {
      address1: string,
      address2: string,
      city: string,
      country: string,
      postcode: string,
      country: stringdob
    },
  },
  voucherCode: string,
  voucherAmount: number,
  orderDate: date,
  items:[
    {
      itemId: number,
      type: string,
      supplierCode: string,
      supplierName: string,
      bookingReference: string,
      supplierPrice: float,
      supplierPriceCurrency: string,
      sellingPrice: float,
      sellingPriceCurrency: string,
      legs: [
        {
          departureDate: datetime,
          arrivalDate: datetime, // can be null of not available
          origin: string,
          destination: string,
          carrier: string,
          flightNumber: string,
          class: string
        }
      ],
      passengers: [
        {
          passengerType: string,
          title: string,
          firstName: string,
          surName: string,
          dob: string,
          gender: string,
          foidType: string
        }
      ]
    }
  ]

}



from How to read and map CSV's multi line header rows using python

No comments:

Post a Comment