List2json

From Federal Burro of Information
Jump to navigationJump to search

list2json.py

#!/usr/bin/env python3
import json

infilename  = "infile.txt"

dict = {}

with open(infilename, 'r') as infile:
  for line in infile:
    a = line.split()
    name = " ".join(a[:len(a)-1])
    level = a[-1]
    dict[name] = level

print(json.dumps(dict))


infile.txt looks like this:

Afterburner 3
Caldari Cruiser 5
Caldari Destroyer 3
Caldari Frigate 3
Capacitor Management 4
Drones 5
Energy Grid Upgrades 5
Gunnery 2
Heavy Assault Cruisers 1
Heavy Missile Specialization 1
Heavy Missiles 5
High Speed Maneuvering 1
Hull Upgrades 5
Light Drone Operation 5
Light Missiles 3
Mechanics 1
Minmatar Drone Specialization 1
Missile Launcher Operation 4
Navigation 3
Power Grid Management 3
Science 1
Shield Upgrades 4
Spaceship Command 5
Tactical Shield Manipulation 4
Weapon Upgrades 5

the out file looks like this:

{"Afterburner": "3", "Caldari Cruiser": "5", "Caldari Destroyer": "3", "Caldari Frigate": "3", "Capacitor Management": "4", "Drones
": "5", "Energy Grid Upgrades": "5", "Gunnery": "2", "Heavy Assault Cruisers": "1", "Heavy Missile Specialization": "1", "Heavy Mis
siles": "5", "High Speed Maneuvering": "1", "Hull Upgrades": "5", "Light Drone Operation": "5", "Light Missiles": "3", "Mechanics":
 "1", "Minmatar Drone Specialization": "1", "Missile Launcher Operation": "4", "Navigation": "3", "Power Grid Management": "3", "Sc
ience": "1", "Shield Upgrades": "4", "Spaceship Command": "5", "Tactical Shield Manipulation": "4", "Weapon Upgrades": "5"}

( one line wrapped )

This is technically valid jason but if you want a prettier output use "jq" "json query" which will friendly format the json. Like this:

 $ python3 list2json.py  | jq
{
  "Afterburner": "3",
  "Caldari Cruiser": "5",
  "Caldari Destroyer": "3",
  "Caldari Frigate": "3",
  "Capacitor Management": "4",
  "Drones": "5",
  "Energy Grid Upgrades": "5",
  "Gunnery": "2",
  "Heavy Assault Cruisers": "1",
  "Heavy Missile Specialization": "1",
  "Heavy Missiles": "5",
  "High Speed Maneuvering": "1",
  "Hull Upgrades": "5",
  "Light Drone Operation": "5",
  "Light Missiles": "3",
  "Mechanics": "1",
  "Minmatar Drone Specialization": "1",
  "Missile Launcher Operation": "4",
  "Navigation": "3",
  "Power Grid Management": "3",
  "Science": "1",
  "Shield Upgrades": "4",
  "Spaceship Command": "5",
  "Tactical Shield Manipulation": "4",
  "Weapon Upgrades": "5"
}