Problem editing javascript file in Python

172 views Asked by At

I'm editing a javascript file in Python, I've done most places but I'm stuck at a critical point, can you help?

The part I'm trying to edit:

                    "589": {
                        p: "no-repeat",
                        c: 97,
                        q: "100% 100%",
                        bS: 420,
                        cP: "garson",
                        r: "none",
                        d: 138,
                        cQ: 1,
                        gg: "0",
                        cR: 1,
                        aP: "pointer",
                        h: "553",
                        i: "garson2",
                        bF: "578",
                        aI: 35,
                        j: "absolute",
                        x: "visible",
                        aA: {
                            a: [{
                                d: 1.1,
                                p: 1,
                                g: 1,
                                f: 1
                            }, {
                                p: 4,
                                h: "474"
                            }, {
                                p: 4,
                                h: "476"
                            }]
                        },
                        k: "div",
                        aJ: 35,
                        dB: "img",
                        z: 23,
                        Q: 16,
                        aK: 35,
                        R: "rgba(0, 0, 0, 0.411)",
                        S: 5,
                        a: 140,
                        aL: 35,
                        T: 4,
                        b: 2
                    }

There are many more blocks like this in the javascript file. i: "garson2" term is also not fixed but I can get this value. Using the term i: "garson2", I want to make r: "none" variable r: "inline" in the block where this term is. How can I do it? Note: The terms between r: "none" and i: "garson2" are also variable.

2

There are 2 answers

1
Liju On BEST ANSWER

You may use positive look ahead assertions for this and use re.sub() to replace.

r: "none"(?=[^{}]+i: "garson2")

Demo

Example

import re
text="""
"589": {
                        p: "no-repeat",
                        c: 97,
                        q: "100% 100%",
                        bS: 420,
                        cP: "garson",
                        r: "none",
                        d: 138,
                        cQ: 1,
                        gg: "0",
                        cR: 1,
                        aP: "pointer",
                        h: "553",
                        i: "garson2",
                        bF: "578",
                        aI: 35,
                        j: "absolute",
                        x: "visible",
                        aA: {
                            a: [{
                                d: 1.1,
                                p: 1,
                                g: 1,
                                f: 1
                            }, {
                                p: 4,
                                h: "474"
                            }, {
                                p: 4,
                                h: "476"
                            }]
                        },
                        k: "div",
                        aJ: 35,
                        dB: "img",
                        z: 23,
                        Q: 16,
                        aK: 35,
                        R: "rgba(0, 0, 0, 0.411)",
                        S: 5,
                        a: 140,
                        aL: 35,
                        T: 4,
                        b: 2
                    }
"""

print(re.sub(r'r: "none"(?=[^{}]+i: "garson2")','r: "inline"',text))

Output

"589": {
                        p: "no-repeat",
                        c: 97,
                        q: "100% 100%",
                        bS: 420,
                        cP: "garson",
                        r: "inline",
                        d: 138,
                        cQ: 1,
                        gg: "0",
                        cR: 1,
                        aP: "pointer",
                        h: "553",
                        i: "garson2",
                        bF: "578",
                        aI: 35,
                        j: "absolute",
                        x: "visible",
                        aA: {
                            a: [{
                                d: 1.1,
                                p: 1,
                                g: 1,
                                f: 1
                            }, {
                                p: 4,
                                h: "474"
                            }, {
                                p: 4,
                                h: "476"
                            }]
                        },
                        k: "div",
                        aJ: 35,
                        dB: "img",
                        z: 23,
                        Q: 16,
                        aK: 35,
                        R: "rgba(0, 0, 0, 0.411)",
                        S: 5,
                        a: 140,
                        aL: 35,
                        T: 4,
                        b: 2
                    }
2
Tunahan Yeniyayla On
    r = re.compile(r'r: "none"(?=[^{}]+i: "garson2")')
    with open('static/js/cafeproje_hype_generated_script.js') as f:
        contents = f.read()

    contents = r.sub(r'r: "none"(?=[^{}]+i: "garson2")','r: "inline"',contents)

    with open('static/js/cafeproje_hype_generated_script.js', 'w') as f:
        f.write(contents)

    return print ("Garson Ekleme İşlemi Tamam\n\nEklenen Garson İsmi:\t{}\nEklenen Garson Sırası:\t{}".format(garson_isim,garson_sira))

I tried it like you said in the code. But I got an error. I apologize for not writing this part in advance, I guess I have extended the time required for a solution without knowing it.

The error I get is:

Traceback (most recent call last):
  File "/Volumes/GoogleDrive/Ortak Drive'lar/Arga Tek/Tunahan/Projeler/ProjeCafeDeneme/garsonekle.py", line 7, in <module>
    garsonislem.garson_ekle(garson_sira,garson_isim,res_adr) #garson eklemek için
  File "/Volumes/GoogleDrive/Ortak Drive'lar/Arga Tek/Tunahan/Projeler/ProjeCafeDeneme/garsonislem.py", line 17, in garson_ekle
    res_ekle(garson_sira,garson_isim,res_adr)
  File "/Volumes/GoogleDrive/Ortak Drive'lar/Arga Tek/Tunahan/Projeler/ProjeCafeDeneme/garsonislem.py", line 48, in res_ekle
    gorunur_yap(garson_sira,garson_isim)
  File "/Volumes/GoogleDrive/Ortak Drive'lar/Arga Tek/Tunahan/Projeler/ProjeCafeDeneme/garsonislem.py", line 81, in gorunur_yap
    contents = r.sub(r'r: "none"(?=[^{}]+i: "garson2")','r: "inline"',contents)
TypeError: 'str' object cannot be interpreted as an integer