Converting 2D arrays into strings
up vote
1
down vote
favorite
I have this function which converts a couple of 2D NumPy arrays into strings and then saves them in a text file (grid
, quality
, roadMap
, fireEffectMap
, hospitalEffectMap
, policeEffectMap
, fireMap
, densityMap
, errorMap
, waterMap
) as well as a couple of other vars (cash
, size
), size being 48, but this is long and ugly. Also, I have 3 different strings/lines for a variable, because each of the values in the waterMap
var can be 3 characters long, e.g. 0-255, that and density, but the rest are 1 character per "slot" in the 2d array. How do I make this more compact, easier to understand/read and also add more in the future if needed?
def save(saves, seed, cash, grid, quality, roadMap, fireEffectMap, hospitalEffectMap, policeEffectMap, fireMap, densityMap, errorMap, waterMap, size):
print("Saving File...")
currentDir = os.getcwd() #Get current directory
os.chdir('saves') #Set the current dir to saves
newFileName = "Save" + str(len(saves) + 1) + ".simcity" #Set the name of the save to Save + biggest number.simcity
file = open(newFileName, "w") #Open/Create a new file
seedString = ""
for i in range(size):
for j in range(size):
seedString = seedString + str(seed[i, j])
file.write(seedString + "n")
file.write(str(cash) + "n") #Store your cash in line 2
gridString = ""
qualityString = ""
roadMapString = ""
fireEffectMapString = ""
hospitalEffectMapString = ""
policeEffectMapString = ""
fireMapString = ""
errorMapString = ""
densityMapStringValue = ""
densityMapString1 = ""
densityMapString2 = ""
waterMapString = ""
waterMapString1 = ""
waterMapString2 = ""
waterMapString3 = ""
for j in range(size):
for i in range(size): #Storing all of the 2d numpy arrays in strings on seperate lines
gridString = gridString + grid[i, j]
qualityString = qualityString + str(quality[i, j])
roadMapString = roadMapString + str(roadMap[i, j])
fireEffectMapString = fireEffectMapString + str(fireEffectMap[i, j])
hospitalEffectMapString = hospitalEffectMapString + str(hospitalEffectMap[i, j])
policeEffectMapString = policeEffectMapString + str(policeEffectMap[i, j])
fireMapString = fireMapString + str(fireMap[i, j])
errorMapString = errorMapString + str(errorMap[i, j])
densityMapStringValue = str(densityMap[i, j])
waterMapStringValue = str(waterMap[i, j])
if len(str(densityMap[i,j])) == 2:
densityMapString1 = densityMapString1 + str(densityMapStringValue[0])
densityMapString2 = densityMapString2 + str(densityMapStringValue[1])
else:
densityMapString1 = densityMapString1 + "0"
densityMapString2 = densityMapString2 + str(densityMapStringValue[0])
if len(str(waterMap[i,j])) == 3:
waterMapString1 = waterMapString1 + str(waterMapStringValue[0])
waterMapString2 = waterMapString2 + str(waterMapStringValue[1])
waterMapString3 = waterMapString3 + str(waterMapStringValue[2])
elif len(str(waterMap[i,j])) == 2:
waterMapString1 = waterMapString1 + "0"
waterMapString2 = waterMapString2 + str(waterMapStringValue[0])
waterMapString3 = waterMapString3 + str(waterMapStringValue[1])
else:
waterMapString1 = waterMapString1 + "0"
waterMapString2 = waterMapString2 + "0"
waterMapString3 = waterMapString3 + str(waterMapStringValue[0])
file.write(gridString + "n")
file.write(qualityString + "n")
file.write(roadMapString + "n")
file.write(fireEffectMapString + "n")
file.write(hospitalEffectMapString + "n")
file.write(policeEffectMapString + "n")
file.write(fireMapString + "n")
file.write(errorMapString + "n")
file.write(densityMapString1 + "n")
file.write(densityMapString2 + "n")
file.write(waterMapString1 + "n")
file.write(waterMapString2 + "n")
file.write(waterMapString3 + "n")
file.close()
os.chdir(currentDir)
python python-3.x numpy pygame
New contributor
add a comment |
up vote
1
down vote
favorite
I have this function which converts a couple of 2D NumPy arrays into strings and then saves them in a text file (grid
, quality
, roadMap
, fireEffectMap
, hospitalEffectMap
, policeEffectMap
, fireMap
, densityMap
, errorMap
, waterMap
) as well as a couple of other vars (cash
, size
), size being 48, but this is long and ugly. Also, I have 3 different strings/lines for a variable, because each of the values in the waterMap
var can be 3 characters long, e.g. 0-255, that and density, but the rest are 1 character per "slot" in the 2d array. How do I make this more compact, easier to understand/read and also add more in the future if needed?
def save(saves, seed, cash, grid, quality, roadMap, fireEffectMap, hospitalEffectMap, policeEffectMap, fireMap, densityMap, errorMap, waterMap, size):
print("Saving File...")
currentDir = os.getcwd() #Get current directory
os.chdir('saves') #Set the current dir to saves
newFileName = "Save" + str(len(saves) + 1) + ".simcity" #Set the name of the save to Save + biggest number.simcity
file = open(newFileName, "w") #Open/Create a new file
seedString = ""
for i in range(size):
for j in range(size):
seedString = seedString + str(seed[i, j])
file.write(seedString + "n")
file.write(str(cash) + "n") #Store your cash in line 2
gridString = ""
qualityString = ""
roadMapString = ""
fireEffectMapString = ""
hospitalEffectMapString = ""
policeEffectMapString = ""
fireMapString = ""
errorMapString = ""
densityMapStringValue = ""
densityMapString1 = ""
densityMapString2 = ""
waterMapString = ""
waterMapString1 = ""
waterMapString2 = ""
waterMapString3 = ""
for j in range(size):
for i in range(size): #Storing all of the 2d numpy arrays in strings on seperate lines
gridString = gridString + grid[i, j]
qualityString = qualityString + str(quality[i, j])
roadMapString = roadMapString + str(roadMap[i, j])
fireEffectMapString = fireEffectMapString + str(fireEffectMap[i, j])
hospitalEffectMapString = hospitalEffectMapString + str(hospitalEffectMap[i, j])
policeEffectMapString = policeEffectMapString + str(policeEffectMap[i, j])
fireMapString = fireMapString + str(fireMap[i, j])
errorMapString = errorMapString + str(errorMap[i, j])
densityMapStringValue = str(densityMap[i, j])
waterMapStringValue = str(waterMap[i, j])
if len(str(densityMap[i,j])) == 2:
densityMapString1 = densityMapString1 + str(densityMapStringValue[0])
densityMapString2 = densityMapString2 + str(densityMapStringValue[1])
else:
densityMapString1 = densityMapString1 + "0"
densityMapString2 = densityMapString2 + str(densityMapStringValue[0])
if len(str(waterMap[i,j])) == 3:
waterMapString1 = waterMapString1 + str(waterMapStringValue[0])
waterMapString2 = waterMapString2 + str(waterMapStringValue[1])
waterMapString3 = waterMapString3 + str(waterMapStringValue[2])
elif len(str(waterMap[i,j])) == 2:
waterMapString1 = waterMapString1 + "0"
waterMapString2 = waterMapString2 + str(waterMapStringValue[0])
waterMapString3 = waterMapString3 + str(waterMapStringValue[1])
else:
waterMapString1 = waterMapString1 + "0"
waterMapString2 = waterMapString2 + "0"
waterMapString3 = waterMapString3 + str(waterMapStringValue[0])
file.write(gridString + "n")
file.write(qualityString + "n")
file.write(roadMapString + "n")
file.write(fireEffectMapString + "n")
file.write(hospitalEffectMapString + "n")
file.write(policeEffectMapString + "n")
file.write(fireMapString + "n")
file.write(errorMapString + "n")
file.write(densityMapString1 + "n")
file.write(densityMapString2 + "n")
file.write(waterMapString1 + "n")
file.write(waterMapString2 + "n")
file.write(waterMapString3 + "n")
file.close()
os.chdir(currentDir)
python python-3.x numpy pygame
New contributor
1
The current question title, which states your concerns about the code, applies to too many questions on this site to be useful. The site standard is for the title to simply state the task accomplished by the code. Please see How to Ask for examples, and revise the title accordingly.
– Toby Speight
Nov 14 at 20:57
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I have this function which converts a couple of 2D NumPy arrays into strings and then saves them in a text file (grid
, quality
, roadMap
, fireEffectMap
, hospitalEffectMap
, policeEffectMap
, fireMap
, densityMap
, errorMap
, waterMap
) as well as a couple of other vars (cash
, size
), size being 48, but this is long and ugly. Also, I have 3 different strings/lines for a variable, because each of the values in the waterMap
var can be 3 characters long, e.g. 0-255, that and density, but the rest are 1 character per "slot" in the 2d array. How do I make this more compact, easier to understand/read and also add more in the future if needed?
def save(saves, seed, cash, grid, quality, roadMap, fireEffectMap, hospitalEffectMap, policeEffectMap, fireMap, densityMap, errorMap, waterMap, size):
print("Saving File...")
currentDir = os.getcwd() #Get current directory
os.chdir('saves') #Set the current dir to saves
newFileName = "Save" + str(len(saves) + 1) + ".simcity" #Set the name of the save to Save + biggest number.simcity
file = open(newFileName, "w") #Open/Create a new file
seedString = ""
for i in range(size):
for j in range(size):
seedString = seedString + str(seed[i, j])
file.write(seedString + "n")
file.write(str(cash) + "n") #Store your cash in line 2
gridString = ""
qualityString = ""
roadMapString = ""
fireEffectMapString = ""
hospitalEffectMapString = ""
policeEffectMapString = ""
fireMapString = ""
errorMapString = ""
densityMapStringValue = ""
densityMapString1 = ""
densityMapString2 = ""
waterMapString = ""
waterMapString1 = ""
waterMapString2 = ""
waterMapString3 = ""
for j in range(size):
for i in range(size): #Storing all of the 2d numpy arrays in strings on seperate lines
gridString = gridString + grid[i, j]
qualityString = qualityString + str(quality[i, j])
roadMapString = roadMapString + str(roadMap[i, j])
fireEffectMapString = fireEffectMapString + str(fireEffectMap[i, j])
hospitalEffectMapString = hospitalEffectMapString + str(hospitalEffectMap[i, j])
policeEffectMapString = policeEffectMapString + str(policeEffectMap[i, j])
fireMapString = fireMapString + str(fireMap[i, j])
errorMapString = errorMapString + str(errorMap[i, j])
densityMapStringValue = str(densityMap[i, j])
waterMapStringValue = str(waterMap[i, j])
if len(str(densityMap[i,j])) == 2:
densityMapString1 = densityMapString1 + str(densityMapStringValue[0])
densityMapString2 = densityMapString2 + str(densityMapStringValue[1])
else:
densityMapString1 = densityMapString1 + "0"
densityMapString2 = densityMapString2 + str(densityMapStringValue[0])
if len(str(waterMap[i,j])) == 3:
waterMapString1 = waterMapString1 + str(waterMapStringValue[0])
waterMapString2 = waterMapString2 + str(waterMapStringValue[1])
waterMapString3 = waterMapString3 + str(waterMapStringValue[2])
elif len(str(waterMap[i,j])) == 2:
waterMapString1 = waterMapString1 + "0"
waterMapString2 = waterMapString2 + str(waterMapStringValue[0])
waterMapString3 = waterMapString3 + str(waterMapStringValue[1])
else:
waterMapString1 = waterMapString1 + "0"
waterMapString2 = waterMapString2 + "0"
waterMapString3 = waterMapString3 + str(waterMapStringValue[0])
file.write(gridString + "n")
file.write(qualityString + "n")
file.write(roadMapString + "n")
file.write(fireEffectMapString + "n")
file.write(hospitalEffectMapString + "n")
file.write(policeEffectMapString + "n")
file.write(fireMapString + "n")
file.write(errorMapString + "n")
file.write(densityMapString1 + "n")
file.write(densityMapString2 + "n")
file.write(waterMapString1 + "n")
file.write(waterMapString2 + "n")
file.write(waterMapString3 + "n")
file.close()
os.chdir(currentDir)
python python-3.x numpy pygame
New contributor
I have this function which converts a couple of 2D NumPy arrays into strings and then saves them in a text file (grid
, quality
, roadMap
, fireEffectMap
, hospitalEffectMap
, policeEffectMap
, fireMap
, densityMap
, errorMap
, waterMap
) as well as a couple of other vars (cash
, size
), size being 48, but this is long and ugly. Also, I have 3 different strings/lines for a variable, because each of the values in the waterMap
var can be 3 characters long, e.g. 0-255, that and density, but the rest are 1 character per "slot" in the 2d array. How do I make this more compact, easier to understand/read and also add more in the future if needed?
def save(saves, seed, cash, grid, quality, roadMap, fireEffectMap, hospitalEffectMap, policeEffectMap, fireMap, densityMap, errorMap, waterMap, size):
print("Saving File...")
currentDir = os.getcwd() #Get current directory
os.chdir('saves') #Set the current dir to saves
newFileName = "Save" + str(len(saves) + 1) + ".simcity" #Set the name of the save to Save + biggest number.simcity
file = open(newFileName, "w") #Open/Create a new file
seedString = ""
for i in range(size):
for j in range(size):
seedString = seedString + str(seed[i, j])
file.write(seedString + "n")
file.write(str(cash) + "n") #Store your cash in line 2
gridString = ""
qualityString = ""
roadMapString = ""
fireEffectMapString = ""
hospitalEffectMapString = ""
policeEffectMapString = ""
fireMapString = ""
errorMapString = ""
densityMapStringValue = ""
densityMapString1 = ""
densityMapString2 = ""
waterMapString = ""
waterMapString1 = ""
waterMapString2 = ""
waterMapString3 = ""
for j in range(size):
for i in range(size): #Storing all of the 2d numpy arrays in strings on seperate lines
gridString = gridString + grid[i, j]
qualityString = qualityString + str(quality[i, j])
roadMapString = roadMapString + str(roadMap[i, j])
fireEffectMapString = fireEffectMapString + str(fireEffectMap[i, j])
hospitalEffectMapString = hospitalEffectMapString + str(hospitalEffectMap[i, j])
policeEffectMapString = policeEffectMapString + str(policeEffectMap[i, j])
fireMapString = fireMapString + str(fireMap[i, j])
errorMapString = errorMapString + str(errorMap[i, j])
densityMapStringValue = str(densityMap[i, j])
waterMapStringValue = str(waterMap[i, j])
if len(str(densityMap[i,j])) == 2:
densityMapString1 = densityMapString1 + str(densityMapStringValue[0])
densityMapString2 = densityMapString2 + str(densityMapStringValue[1])
else:
densityMapString1 = densityMapString1 + "0"
densityMapString2 = densityMapString2 + str(densityMapStringValue[0])
if len(str(waterMap[i,j])) == 3:
waterMapString1 = waterMapString1 + str(waterMapStringValue[0])
waterMapString2 = waterMapString2 + str(waterMapStringValue[1])
waterMapString3 = waterMapString3 + str(waterMapStringValue[2])
elif len(str(waterMap[i,j])) == 2:
waterMapString1 = waterMapString1 + "0"
waterMapString2 = waterMapString2 + str(waterMapStringValue[0])
waterMapString3 = waterMapString3 + str(waterMapStringValue[1])
else:
waterMapString1 = waterMapString1 + "0"
waterMapString2 = waterMapString2 + "0"
waterMapString3 = waterMapString3 + str(waterMapStringValue[0])
file.write(gridString + "n")
file.write(qualityString + "n")
file.write(roadMapString + "n")
file.write(fireEffectMapString + "n")
file.write(hospitalEffectMapString + "n")
file.write(policeEffectMapString + "n")
file.write(fireMapString + "n")
file.write(errorMapString + "n")
file.write(densityMapString1 + "n")
file.write(densityMapString2 + "n")
file.write(waterMapString1 + "n")
file.write(waterMapString2 + "n")
file.write(waterMapString3 + "n")
file.close()
os.chdir(currentDir)
python python-3.x numpy pygame
python python-3.x numpy pygame
New contributor
New contributor
edited 2 days ago
Jamal♦
30.2k11115226
30.2k11115226
New contributor
asked Nov 14 at 18:29
Skezza
61
61
New contributor
New contributor
1
The current question title, which states your concerns about the code, applies to too many questions on this site to be useful. The site standard is for the title to simply state the task accomplished by the code. Please see How to Ask for examples, and revise the title accordingly.
– Toby Speight
Nov 14 at 20:57
add a comment |
1
The current question title, which states your concerns about the code, applies to too many questions on this site to be useful. The site standard is for the title to simply state the task accomplished by the code. Please see How to Ask for examples, and revise the title accordingly.
– Toby Speight
Nov 14 at 20:57
1
1
The current question title, which states your concerns about the code, applies to too many questions on this site to be useful. The site standard is for the title to simply state the task accomplished by the code. Please see How to Ask for examples, and revise the title accordingly.
– Toby Speight
Nov 14 at 20:57
The current question title, which states your concerns about the code, applies to too many questions on this site to be useful. The site standard is for the title to simply state the task accomplished by the code. Please see How to Ask for examples, and revise the title accordingly.
– Toby Speight
Nov 14 at 20:57
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Skezza is a new contributor. Be nice, and check out our Code of Conduct.
Skezza is a new contributor. Be nice, and check out our Code of Conduct.
Skezza is a new contributor. Be nice, and check out our Code of Conduct.
Skezza is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f207671%2fconverting-2d-arrays-into-strings%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
The current question title, which states your concerns about the code, applies to too many questions on this site to be useful. The site standard is for the title to simply state the task accomplished by the code. Please see How to Ask for examples, and revise the title accordingly.
– Toby Speight
Nov 14 at 20:57