How to convert to json file formate in python
<p>Here is my output:</p>

<pre><code>xyz information
+-----+------+------+
| A   | B    |  C   |
+-----+------+------+
| 23  |  76  | 87   |
| 76  |  36  | 37   |
| 83  |  06  | 27   |
+-----+------+------+
</code></pre>

<p>I want to convert this output to json format in python
can anybody suggest how to do that.</p>

<p>Given</p>

<pre><code>xyz = '''+-----+------+------+
| A   | B    |  C   |
+-----+------+------+
| 23  |  76  | 87   |
| 76  |  36  | 37   |
| 83  |  06  | 27   |
+-----+------+------+'''
</code></pre>

<p>Do</p>

<pre><code>import json
import collections

xyz_rows = [map(str.strip, row.split('|')[1:-1]) for row in xyz.split('\n') if '|' in row]
xyz_cols = collections.OrderedDict() # OrderedDict to preserve column order
for column in zip(*xyz_rows): # rows to columns
    xyz_cols[column[0]] = column[1:]

xyz_json = json.dumps(xyz_cols)
</code></pre>

<p><code>xyz_json</code> contains</p>

<pre><code>'{"A": ["23", "76", "83"], "B": ["76", "36", "06"], "C": ["87", "37", "27"]}'
</code></pre>

python json python-2.7