I'm quite new to python 3, and I'm developing a REST API to format some characters in a JSON of numerous strings (thousands sometimes) , the JSON has this structure:
[
[
"city",
"Street 158 No 96"
],
[
"city",
"st 144 11a 11 ap 104"
],
[
"city",
"Street83 # 85 - 22"
],
[
"city",
"str13 #153 - 81"
],
[
"city",
"street1h # 24 - 29"
]
]
So what I did to replace this on excel macros was.
text = Replace(text, "st", " street ", , , vbTextCompare)
For i = 0 To 9 Step 1
text = Replace(text, "street" & i, " street " & i, , , vbTextCompare)
text = Replace(text, "st" & i, " street " & i, , , vbTextCompare)
This would format every cell to 'street #' no matter the number, now the problem is when I try to do this with python, right now I have learned how to replace multiple values on a list like so:
addressList= []
for address in request.json:
address = [element
.replace('st', 'street ')
.replace('street1', 'street 1')
.replace('street2', 'street 2')
.replace('street3', 'street 3')
.replace('street4', 'street 4')
.replace('street5', 'street 5')
#and so on for st too
for element in address]
addressList.append(address)
This method is not just long but also really ugly, I'd like to do something like what I had before, but I can't seem to be able to use a for inside the replace, should I do it outside?
Thank you for helping.
--EDIT--
edited the json format so it's valid.
tried both revliscano and The fourth bird's replies they both work, currently i'm using revliscano's method as it allows me to create the list from my original Json in just 'one line'
{}surrounds objects, which are inkey: valueformat.