1

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 import.json.

The file looks like

  Source    Target  Value
B cells Streptococcus pneumoniae    226
B cells Candida albicans    136
B cells Mycoplasma  120

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

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()

Wants the output to be like

{
  "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"
        }
  ]
}
sp2
  • 509
  • 1
  • 5
  • 13

1 Answers1

1

You can read it as a csv file and convert it into json. 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 , instead of space.

the working code for what you're trying,

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)

output,

[
   {
      "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"
   }
]

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

praba230890
  • 2,192
  • 21
  • 37