Friday 4 October 2019

Post 74: Convert datasets in text file into json file with python

Sometimes you want to convert datasets from text file to json.

Let's say you have the following input text file:

 Japan 83.74 80.91 86.58
 Italy 83.31 80 86.49
 Switzerland 82.84 80.27 85.23
 Singapore 82.66 80.43 84.74
 Israel 82.64 79.59 85.61
 Iceland 82.3 80.73 83.84
 Spain 82.28 79.42 85.05
 Australia 83.42 80.33 86.56
 Hong Kong 82.07 80.18 83.82
 Sweden 81.93 80.1 83.71
 France (metropol.) 81.85 78.76 84.87
 Canada 81.78 79.69 83.78
...

The datasets above have 4 columns. The first column has also a heading space that we need to get rid of. The columns are separated by tabs (\t) and each line is sepearted by line breaks (\n). To convert this datasets into and json output file, we write the following python script:

import json
import re

f=open("input.txt", "r")

arr = []
jsonResult = []

if f.mode == 'r':
    contents =f.read()
    arr = re.split(r'\n+', contents)
    print(arr)

arr = arr[:-1] # remove last element

for x in arr:
    line = re.split(r'\t+', x)
    print(line)
    country = line[0].strip() # remove heading and trailing spaces spaces
    expected_age_overall = line[1]
    expected_age_male = line[2]
    expected_age_female = line[3]
    json_item = {
        "country": country,
        "expected_age_overall": expected_age_overall,
        "expected_age_male": expected_age_male,
        "expected_age_female": expected_age_female
    }
    jsonResult.append(json_item)

 
print(jsonResult) # display the result

with open('output.json', 'w') as json_file:
    json.dump(jsonResult, json_file)


No comments:

Post a Comment

Tweet