Converting a Text file to JSON format using Python
<p>I am not new to programming but not good at python data structures. I would like to know a way to convert a text file into JSON format using python since I heard using python the task is much easier with a module called <code>import.json</code>.</p>

<p>The file looks like </p>

<pre><code>  Source    Target  Value
B cells Streptococcus pneumoniae    226
B cells Candida albicans    136
B cells Mycoplasma  120
</code></pre>

<p>For the first line "B cells" is the source, target is the "Streptococcus pneumoniae" and value is "226". I just started with the code, but couldnot finish it. Please help</p>

<pre><code>import json
prot2_names = {}
tmpfil = open("file.txt", "r");
for lin in tmpfil.readlines():
    flds = lin.rstrip().split("\t")
    prot2_names[flds[0]] = "\"" + flds[1] + "\""
    print prot2_names+"\t",
tmpfil.close()
</code></pre>

<p>Wants the output to be like</p>

<pre><code>{
  "nodes": [
    {
      "name": "B cells"
    },
    {
      "name": "Streptococcus pneumoniae"
    },
    {
      "name": "Candida albicans"
    },
    {
      "name": "Mycoplasma"
    },
    {
    "links": [
    {
      "source": 0,
      "target": 1,
      "value": "226"
    },
    {
      "source": 0,
      "target": 2,
      "value": "136"
    },
    {
      "source": 0,
      "target": 3,
      "value": "120"
        }
  ]
}
</code></pre>

<p>You can read it as a <code>csv</code> file and convert it into <code>json</code>. But, be careful with spaces as you've used it as separator, the values with spaces should be carefully handled. Otherwise, if possible make the separator <code>,</code> instead of <code>space</code>.</p>

<p>the working code for what you're trying,</p>

<pre><code>import csv
import json

with open('file.txt', 'rb') as csvfile:
    filereader = csv.reader(csvfile, delimiter=' ')
    i = 0
    header = []
    out_data = []
    for row in filereader:
        row = [elem for elem in row if elem]
        if i == 0:
            i += 1
            header = row
        else:
            row[0:2] = [row[0]+" "+row[1]]
            _dict = {}
            for elem, header_elem in zip(row, header):
                _dict[header_elem] = elem
            out_data.append(_dict)

print json.dumps(out_data)
</code></pre>

<p>output,</p>

<pre><code>[
   {
      "Source":"B cells",
      "Target":"Streptococcus",
      "Value":"pneumoniae"
   },
   {
      "Source":"B cells",
      "Target":"Candida",
      "Value":"albicans"
   },
   {
      "Source":"B cells",
      "Target":"Mycoplasma",
      "Value":"120"
   },
   {
      "Source":"B cells",
      "Target":"Neisseria",
      "Value":"111"
   },
   {
      "Source":"B cells",
      "Target":"Pseudomonas",
      "Value":"aeruginosa"
   }
]
</code></pre>

<p>UPDATE: Just noticed your updated question with <code>json</code> sample that you require. Hope, you could build it with the above example I've written.</p>

python json