title: Pretty print a JSON.dump 2d array in Python
body: <p>I'm writing a python tile map editor. I was using a self designed code to write and parse some files but the code was very confusing, so I switched to JSON for simplicity. </p>
  
  <p>My json file is hand made like this:</p>
  
  <pre><code>{ "Level" :
  
       {
  
      "levelName": "simpleLevel",
  
      "background":   [[12, 2,12, 2, 1, 2, 3, 3, 1, 1],
                       [ 1, 1, 7, 8, 5, 6, 7, 8,12, 3],
                       [ 1, 3, 1, 3,12,10,10, 1,12,12],
                       [ 2,12, 0, 4,10, 3,12, 2,12,12],
                       [12,12, 1, 1,10, 3,12, 2,12, 1],
                       [12,12,12, 0,10, 2, 1,12, 1,12],
                       [ 3,12, 3,12, 0, 2, 2,12,12, 3],
                       [ 1,12, 1,12, 1, 1,12,12, 3,12],
                       [ 3,12, 0,12,12,12,12,12, 3, 3],
                       [12, 3, 1, 2, 3,12,12,12, 1,12]],
  
      "foreground":   [[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                       [ 0, 2, 0, 0, 0, 0, 0, 0, 0, 0],
                       [ 0, 3, 0, 0, 0, 0, 0, 0, 0, 0],
                       [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                       [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                       [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                       [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                       [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                       [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                       [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]
      }
  }
  </code></pre>
  
  <p>But, when I use <code>json.dump(self.x, f)</code> my output is in a single line, and when I try to use these parameter <code>sort_keys=True, indent=4, separators=(',', ': ')</code> I get a thousand linefeeds..</p>
  
  <p>I just want something similar with my handmade file because it's easier to read and edit by hand. I could find some solutions to simple arrays but nothing that worked for 2d arrays. Is there a simple answer or should I code myself? </p>
  
  <p>edit:</p>
  
  <p>coded myself (with some help from the web..)</p>
  
  <pre><code>def fwriteKeyVals(data, f, indent=0):
      if isinstance(data, list):
          f.write( "\n" + "    " * indent + "[" )  
          for i in range(len(data) ):  
              if ( i == 0):
                  f.write( "[" )  
              else:
                  f.write( "    " * indent + " [" )
              for j in range(len(data[0])):  
                  f.write( "%3d" % data[i][j] )  
                  f.write( "," ) if j != len(data[0])-1 else (f.write( "]," ) if i != len(data)-1 else f.write( "]" ))  
              f.write( "\n" ) if i != len(data)-1 else f.write( "]" )  
      elif isinstance(data, dict):
          f.write( "\n" + "    " * indent + "{" )
          for k, v in data.iteritems():
              f.write( "\n" + "    " * indent + "\"" + k + "\"" + ": ")
              fwriteKeyVals(v, f, indent + 1)
              if( data.keys()[-1] != k):
                   f.write( "," )
          f.write( "\n" + "    " * indent + "}" )
      else:
          f.write("\"" + data + "\"")
  </code></pre>
  
  <p>Usage is:</p>
  
  <pre><code>fwriteKeyVals(x, f)
  </code></pre>
  
  <p>Where <code>x = json.load(f)</code> and <code>f</code> is the file I've loaded with <code>f = open( map , "wb" )</code>.</p>