title: 3 CSV files into a single JSON
body: <p>I have 3 csv files, loan, customer and security, each of these files are quite large (800k+ rows).  Each file is linked by a single column uniqueid.  My aim is to create a single JSON file, the code below achieves this, however, it is very slow.</p>
  <p>My question is how can i achieve this result faster?</p>
  <pre><code>import csv
  import json
  
  def multicsvtojson():
  
  loanscsvfile = open('C:\\***\\loan.csv', 'r')
  custcsvfile = open('C:\\***\\customer.csv', 'r')
  securcsvfile = open('C:\\***\\security.csv', 'r')
  
  loansreader = csv.DictReader(loanscsvfile, delimiter=',')
  custreader = csv.DictReader(custcsvfile, delimiter=',')
  securreader = csv.DictReader(securcsvfile, delimiter=',')
  
  jsonfile = open('test.json', 'w')
  #ready json file
  
  output = []
  loanscount = 0
  #total loan count
  for loansrow in loansreader:
      loansrow['customers'] = []
      loansrow['securities'] = []
  
      output.append(loansrow)
      custcsvfile.seek(0)
      securcsvfile.seek(0)
      for custrow in custreader:
          if (loansrow[&quot;UniqueID&quot;] == custrow[&quot;UniqueID&quot;]):
              loansrow['customers'].append(custrow) 
      for securrow in securreader:
          if (loansrow[&quot;UniqueID&quot;] == securrow[&quot;UniqueID&quot;]):
              loansrow['securities'].append(securrow)
  
      loanscount = loanscount + 1 #increment the loan counter
      print(loanscount)
  
  total = {}
  total['total'] = loanscount
  output.insert(0, total)
  
  json.dump(output, jsonfile, indent=4)
  </code></pre>
  <p>The current output is as follow</p>
  <pre><code>[{
      &quot;total&quot;: 2
  },
  {
      &quot;uniqueID&quot;: &quot;&quot;,
      &quot;uniqueID2&quot;: &quot;&quot;,
      &quot;colA&quot;: &quot;&quot;,
      &quot;colB&quot;: &quot;&quot;,
      &quot;colC&quot;: &quot;&quot;,
      &quot;colD&quot;: &quot;&quot;,
      &quot;customers&quot;: [
          {
              &quot;uniqueID&quot;: &quot;&quot;,
              &quot;custID&quot;: &quot;&quot;,
              &quot;colA&quot;: &quot;&quot;,
              &quot;colB&quot;: &quot;&quot;,
          }
      ],
      &quot;securities&quot;: [
          {
              &quot;uniqueID&quot;: &quot;&quot;,
              &quot;secuID&quot;: &quot;&quot;,
              &quot;colA&quot;: &quot;&quot;,
              &quot;colB&quot;: &quot;&quot;
          }
      ]
  },
  {
      &quot;uniqueID&quot;: &quot;&quot;,
      &quot;uniqueID2&quot;: &quot;&quot;,
      &quot;colA&quot;: &quot;&quot;,
      &quot;colB&quot;: &quot;&quot;,
      &quot;colC&quot;: &quot;&quot;,
      &quot;colD&quot;: &quot;&quot;,
      &quot;customers&quot;: [
          {
              &quot;uniqueID&quot;: &quot;&quot;,
              &quot;custID&quot;: &quot;&quot;,
              &quot;colA&quot;: &quot;&quot;,
              &quot;colB&quot;: &quot;&quot;,
          },
          {
              &quot;uniqueID&quot;: &quot;&quot;,
              &quot;secuID&quot;: &quot;&quot;,
              &quot;colA&quot;: &quot;&quot;,
              &quot;colB&quot;: &quot;&quot;
          }
      ],
      &quot;securities&quot;: [
          {
              &quot;uniqueID&quot;: &quot;&quot;,
              &quot;secuID&quot;: &quot;&quot;,
              &quot;colA&quot;: &quot;&quot;,
              &quot;colB&quot;: &quot;&quot;
          },
          {
              &quot;uniqueID&quot;: &quot;&quot;,
              &quot;secuID&quot;: &quot;&quot;,
              &quot;colA&quot;: &quot;&quot;,
              &quot;colB&quot;: &quot;&quot;
          }
      ]
  }
  </code></pre>
  <p>}]</p>