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"
        }
  ]
}
3
  • 1
    Have you tried doing it by hand first? Commented Sep 14, 2016 at 4:47
  • 1
    What do you want the JSON output to look like? The basic idea is to parse your input file into a hierarchy of Python lists and dicts, then call json.dumps(top-level list or dict) to generate a string of JSON. Commented Sep 14, 2016 at 4:48
  • 1
    Why did you import json, if you don't use it? Commented Sep 14, 2016 at 4:52

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.