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)









share|improve this question









New contributor




Skezza is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
















  • 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















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)









share|improve this question









New contributor




Skezza is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
















  • 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













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)









share|improve this question









New contributor




Skezza is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











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






share|improve this question









New contributor




Skezza is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




Skezza is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 2 days ago









Jamal

30.2k11115226




30.2k11115226






New contributor




Skezza is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked Nov 14 at 18:29









Skezza

61




61




New contributor




Skezza is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Skezza is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Skezza is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.








  • 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




    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















active

oldest

votes











Your Answer





StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
});
});
}, "mathjax-editing");

StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "196"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});






Skezza is a new contributor. Be nice, and check out our Code of Conduct.










 

draft saved


draft discarded


















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






























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.










 

draft saved


draft discarded


















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.















 


draft saved


draft discarded














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





















































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







Popular posts from this blog

Quarter-circle Tiles

build a pushdown automaton that recognizes the reverse language of a given pushdown automaton?

Mont Emei