Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import re
- import json
- count=1 # Counter
- # Regex to isolate data from document into named capture groups
- rtrregex = "^(?:(?:\s+\d+\:\s+(?P<jumpDistance>[0-9]+\.[0-9]{2})\s)|\s+)(?P<systemName>\S+.*(?=\s\|\s))\s\|\s(?P<planetName>(?<=\s\|\s).+(?=\s\())\s\((?P<planetDistance>(?<=\()\d+(?=\)))\)\s(?P<planetType>[A-Z]{3})$"
- jsonfile = open('planetList.json', 'w') # JSON output filename
- jsonoutput = []
- with open('list.txt') as f: # Open original document and split into lines
- for line in f:
- if re.match(rtrregex, line): # Ignore lines that do not contain a match to the regex
- rtrclean = re.match(rtrregex, line) # Strip all but the data matching the capture groups
- rtrdict = rtrclean.groupdict()# Convert matches to dict named after capture groups
- if not rtrdict['jumpDistance']: # Make null values in this key 0
- rtrdict['jumpDistance'] = 0
- rtrdict['num'] = count # Every object will be numbered sequentially
- rtrdict['jumpDistance'] = float(rtrdict['jumpDistance']) # Convert string containing number with decimal to float
- rtrdict['planetDistance'] = int(rtrdict['planetDistance']) # Convert string containing number to int
- rtrdict['planetName'] = rtrdict['systemName']+" "+rtrdict['planetName'] # Prepend systemName value to planetName value
- jsonoutput.append(rtrdict) # Add dictionary object to jsonoutput list
- count = count+1 # Increase count by one for next loop
- #rtrjson = json.dumps(rtrdict) # Debugging
- #print(rtrjson) # Debugging
- json.dump(jsonoutput, jsonfile, separators=(',',':')) # Write completed list to JSON file, minified
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement