Sign in to follow this  
Randall Perkins

Python 3.3 Need help with a file

Recommended Posts

This is the file called convert.py
import json, string, re

json_data=open('Route 100.json')

data = json.load(json_data)
data_01 = data['layers']

map_name = data_01[0]['name']
map_width = data_01[0]['width'] * 32;
map_height = data_01[0]['height'] * 32;
map_data = data_01[0]['data']

print("Map Name   : ", map_name)
print("Map Width  : ", map_width)
print("Map Height : ", map_height)
print("Map Output -")
print(map_data)
print("Converted Output")

new_map_data = map_data

def replace_all(text, dic):
    for i, j in dic.iteritems():
        text = text.replace(i,j)
    return text

reps = {'[':'', ' ':'', ']':''}

output = replace_all(new_map_data, reps)


print(output)



json_data.close()

input("\n\nPress any button to exit")

 

 

and i need to replace the [ and ] and blank spaces in new_map_data or map_data

im really new to python so if you could fix this for me it would mean a lot biggrin.png ill attach the file i was using

 

 

NOTE i had to rename 'Route 100.json' to 'Route 100.txt' since gamedev dont allow json file uploads

 

Edited by MrPhoenix

Share this post


Link to post
Share on other sites

this is the best i got to

 

import json, string

input_file = "input.json"
json_data=open(input_file)
output_file = "output.map"

data = json.load(json_data)
data_01 = data['layers']

map_name = data_01[0]['name']
map_width = data_01[0]['width'] * 32;
map_height = data_01[0]['height'] * 32;
map_data = data_01[0]['data']

print("Map Name   : ", map_name)
print("Map Width  : ", map_width)
print("Map Height : ", map_height)
print("Map Output -")
print(map_data)
print("Converted Output")


new_map_data = str(map_data)


    
data = ''
for i in new_map_data:
    if i == ' ':
        print("")
    elif i == '[':
        print("")
    elif i == ']':
        print("")
    else:
        data += i

print("New Format")
print(data)

file = open(output_file, "w")
file.write(map_name)
file.write(',')
file.write(str(map_width))
file.write(',')
file.write(str(map_height))
file.write(',')
file.write(data)
file.close()

json_data.close()

 

i would like to be able to drag a file on to it and output the filename,map in the same folder

and i dont know if i need all the print() or not 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this