Jump to content
  • Advertisement
Sign in to follow this  
Randall Perkins

Python 3.3 Need help with a file

This topic is 2068 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement

You don't have to replace anything. map_data is an array not a string. you can access every entry with the [id] operator.

 

so like map_data[0]

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!