0

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'

2
  • 2
    That's not valid JSON structure. {} surrounds objects, which are in key: value format. Commented Jul 28, 2020 at 23:34
  • You can check your JSON format valid or not in this link jsonlint.com Commented Jul 29, 2020 at 1:15

3 Answers 3

1

Instead of using multiple replace calls, you could use a pattern matching st with optional reet and an optional space, then capture 1+ digits in a group.

\bst(?:reet)? ?(\d+)\b

Regex demo | Python demo

In the replacement use the capturing group street \1 using re.sub

Example code for a single element

import re
element = re.sub(r"\bst(?:reet)? ?(\d+)\b", r"street \1", "st 5")
print (element)

Output

street 5
Sign up to request clarification or add additional context in comments.

Comments

0

I would use a regular expression to solve this. Try with the following

import re

address_list = [[re.sub(r'(?:st ?(\d)?\b)|(?:street(\d))', r'street \1', element)
                for element in address]
                for address in request.json]

Comments

0

You can use regular expressions mixed with dictionary to make it faster.

I use a function like this in one of my programs

import re
def multiple_replace(adict, text):
    regex = re.compile("|".join(map(re.escape, adict.keys())))   
    return regex.sub(lambda match: adict[match.group(0)], text)

adict is the dictionary in which you have the mappings of the charaters you want to replace.

For you, it can be

adict = {
    'street1­': 'street 1'
    'street2':'street 2',
    'street3': 'street 3',
    'street4': 'street 4',
    'street5­': 'street 5',
}

Of course you can't use the exact same function. You will have to write another regular expression according to your needs, like @The fourth bird did

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.