1

I have problem with replace some string with upper or lower case

Its my test script:

translate=[{'NO': 'frontsplitter', 'DK': 'frontlæbe'}, {'NO': 'diffuser', 'DK': 'diffusor'}, {'NO': 'gitter', 'DK': 'grill'}, {'NO': 'lettmetallfelger', 'DK': 'letmetalfælge'}, {'NO': 'stålfelger', 'DK': 'stålfælge'}, {'NO': 'atv felger', 'DK': 'atv fælge'}, {'NO': 'dekk', 'DK': 'dæk'}, {'NO': 'motorcykeldæk', 'DK': 'Motorcykeldæk & scooterdæk'}, {'NO': 'Go-cart-dæk', 'DK': 'gokartdæk'}, {'NO': 'ophæng', 'DK': 'hjulophæng'}, {'NO': 'parhardstag', 'DK': 'parhardstang'}, {'NO': 'stabstag', 'DK': 'stabilisator'}, {'NO': 'styredempere', 'DK': 'styrdæmpere'}, {'NO': 'Støddæmper\xa0 ', 'DK': 'Støddæmpere'}, {'NO': 'tillinger', 'DK': 'bøsning'}, {'NO': 'ogre varer', 'DK': 'andre varer'}, {'NO': 'Off-road udstyr', 'DK': '4x4 tilbehør'}, {'NO': 'drivlinje', 'DK': 'transmissionsystem'}, {'NO': 'ophæng', 'DK': 'hjulophæng'}, {'NO': 'skærmtiløger', 'DK': 'Skærm-kit'}, {'NO': 'vinsjer', 'DK': 'spil'}, {'NO': 'vinsjutstyr', 'DK': 'spiludstyr'}, {'NO': 'bærbar vinsjer', 'DK': 'bærbare spil'}, {'NO': 'vinsjplater', 'DK': 'spilmonteringsplade'}, {'NO': 'afstogsstykker', 'DK': 'Afstandsstykker\n'}, {'NO': 'kofangere', 'DK': 'kofanger'}, {'NO': 'oget tilbehør', 'DK': 'Andre tilbehør'}]


def translating(string):
    print(translate)
    print(string)

    for row in translate:
        NO=row["NO"]
        DK = row["DK"]


        string=string.replace(NO,DK)
    print(string)
    return string


translating("Dekk Maxxis M8080 Mudzilla 33x13.5-15 110L")

how i can ignore case and replace word in this exaple shoud replace Dekk to dæk or when Dekk replace to Dæk and when dekk then replace to dæk

I need duplicate this on array?

2
  • what is your expected output? Commented Jun 4, 2020 at 6:07
  • Dæk Maxxis M8080 Mudzilla 33x13.5-15 110L or When string "dekk Maxxis M8080 Mudzilla 33x13.5-15 110L" then dæk Maxxis M8080 Mudzilla 33x13.5-15 110L ofc its exaple some words can be between some other words Commented Jun 4, 2020 at 6:10

3 Answers 3

2

It seems that you don't pay intrest in maintaining the same case for your output string.

You can simply use the

string.casefold()

option in python to remove all case distinctions present in a python string.

So this is how your function will look

def translating(string):
string = string.casefold()


for row in translate:
    NO=row["NO"]
    DK = row["DK"]


    string=string.replace(NO,DK)
print(string)

You can also use string.upper() to convert the whole string to uppercase or string.lower() to convert the whole string to lowercase and then use string.replace().But in these methods the values stored in the "NO" keys have to match with the respective case.

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

1 Comment

This is perfect case handling. Upvoting this try two cases it working. @small-atom try this.
1

I would check to see if the first letter is a capital letter, if it is convert it to lower case, replace and then capitalize it again. In the case the word isn't capitalized you can just replace.

def translating(string):
        print(translate)
        print(string)

        for row in translate:
            NO=row["NO"]
            DK = row["DK"]

            if string[0].isupper() :
               string=string.lower().replace(NO,DK).capitalize()
            else:
               string=string.replace(NO,DK)

        print(string)
        return string


    translating("Dekk Maxxis M8080 Mudzilla 33x13.5-15 110L")

UPDATE

def translating(string):
    print(translate)
    print(string)

    for row in translate:
        NO=row["NO"]
        DK = row["DK"]

        if string.find(NO.capitalize()):
            string=string.replace(NO,DK.capitalize())

        string=string.replace(NO,DK)

    print(string)
    return string

2 Comments

but it will not works when exaple: translating("maxxis Dekk M8080 Mudzilla 33x13.5-15 110L")
sorry about that i didn't try out my solution i think the updated version should work :)
1

The string type doesn't support this. You're probably best off using the regular expression submethod with the re.IGNORECASE option. Package for that is "re".

1 Comment

Use comment section to recommend. Answer block add your solution.

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.