Jump to content

View more

Image of the Day

Adding some finishing touches...
Follow us for more
#screenshotsaturday #indiedev... by #MakeGoodGames https://t.co/Otbwywbm3a
IOTD | Top Screenshots

The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.


Sign up now

Python 3.3 Need help with a file

4: Adsense

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.


  • You cannot reply to this topic
2 replies to this topic

#1 MrPhoenix   Members   

134
Like
0Likes
Like

Posted 22 January 2013 - 01:09 PM

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

 

Attached Files


Edited by MrPhoenix, 22 January 2013 - 02:32 PM.


#2 Sudi   Members   

761
Like
0Likes
Like

Posted 23 January 2013 - 05:40 AM

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]



#3 MrPhoenix   Members   

134
Like
0Likes
Like

Posted 24 January 2013 - 09:34 AM

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 






Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.