import json
import sys


result = {}

INDEX = {
    "AREA": 5 + 3 * 3 + 3 * 3,
    "CITY": 5 + 3 * 3,
    "CODE": 5
}

for line in open("ORIGIN.txt"):
    code = line[:INDEX["CODE"]]

    city = line[INDEX["CODE"]: INDEX["CITY"]]
    if not city in result: result[city] = {}

    area = line[INDEX["CITY"]: INDEX["AREA"]]
    if not area in result[city]: result[city][area] = {}

    line = line[line.find(" "):].strip()

    road = line[:line.find(" ")]
    condition = line[line.find(" "):].replace("　", "").strip()


json.dump(result, open("zipcode.json", "w"),
    ensure_ascii=False,
    indent=4
)

sys.exit(0)

import json
import sys


result = {}

INDEX = {
    "AREA": 5 + 3 * 3 + 3 * 3,
    "CITY": 5 + 3 * 3,
    "CODE": 5
}

for line in open("ORIGIN.txt"):
    code = line[:INDEX["CODE"]]

    city = line[INDEX["CODE"]: INDEX["CITY"]]
    if not city in result: result[city] = {}

    area_index = INDEX["AREA"]
    if line[INDEX["AREA"]] != " ": area_index += 3
    area = line[INDEX["CITY"]: area_index].strip()
    if not area in result[city]: result[city][area] = {}

    line = line[area_index:].strip()

    road = line.split(" ")[0]
    if len(line.split(" ")) == 1: road = line[:-3]
    if not road in result[city][area]: result[city][area][road] = {}

    condition = line[line.find(" "):].replace("　", "").strip()


json.dump(result, open("zipcode.json", "w"),
    ensure_ascii=False,
    indent=4
)

sys.exit(0)

Fix name parsing for roads
Fix name parsing for roads

  import json
  import sys
  
  
  result = {}
  
  INDEX = {
      "AREA": 5 + 3 * 3 + 3 * 3,
      "CITY": 5 + 3 * 3,
      "CODE": 5
  }
  
  for line in open("ORIGIN.txt"):
      code = line[:INDEX["CODE"]]
  
      city = line[INDEX["CODE"]: INDEX["CITY"]]
      if not city in result: result[city] = {}
  
-     area = line[INDEX["CITY"]: INDEX["AREA"]]
+     area_index = INDEX["AREA"]
+     if line[INDEX["AREA"]] != " ": area_index += 3
+     area = line[INDEX["CITY"]: area_index].strip()
      if not area in result[city]: result[city][area] = {}
  
-     line = line[line.find(" "):].strip()
+     line = line[area_index:].strip()
  
-     road = line[:line.find(" ")]
+     road = line.split(" ")[0]
+     if len(line.split(" ")) == 1: road = line[:-3]
+     if not road in result[city][area]: result[city][area][road] = {}
+ 
      condition = line[line.find(" "):].replace("　", "").strip()
  
  
  json.dump(result, open("zipcode.json", "w"),
      ensure_ascii=False,
      indent=4
  )
  
  sys.exit(0)
  
Fix name parsing for roads
## Code Before:
import json
import sys


result = {}

INDEX = {
    "AREA": 5 + 3 * 3 + 3 * 3,
    "CITY": 5 + 3 * 3,
    "CODE": 5
}

for line in open("ORIGIN.txt"):
    code = line[:INDEX["CODE"]]

    city = line[INDEX["CODE"]: INDEX["CITY"]]
    if not city in result: result[city] = {}

    area = line[INDEX["CITY"]: INDEX["AREA"]]
    if not area in result[city]: result[city][area] = {}

    line = line[line.find(" "):].strip()

    road = line[:line.find(" ")]
    condition = line[line.find(" "):].replace("　", "").strip()


json.dump(result, open("zipcode.json", "w"),
    ensure_ascii=False,
    indent=4
)

sys.exit(0)

## Instruction:
Fix name parsing for roads
## Code After:
import json
import sys


result = {}

INDEX = {
    "AREA": 5 + 3 * 3 + 3 * 3,
    "CITY": 5 + 3 * 3,
    "CODE": 5
}

for line in open("ORIGIN.txt"):
    code = line[:INDEX["CODE"]]

    city = line[INDEX["CODE"]: INDEX["CITY"]]
    if not city in result: result[city] = {}

    area_index = INDEX["AREA"]
    if line[INDEX["AREA"]] != " ": area_index += 3
    area = line[INDEX["CITY"]: area_index].strip()
    if not area in result[city]: result[city][area] = {}

    line = line[area_index:].strip()

    road = line.split(" ")[0]
    if len(line.split(" ")) == 1: road = line[:-3]
    if not road in result[city][area]: result[city][area][road] = {}

    condition = line[line.find(" "):].replace("　", "").strip()


json.dump(result, open("zipcode.json", "w"),
    ensure_ascii=False,
    indent=4
)

sys.exit(0)
