no viable alternative at input '"....

SyntaxError: no viable alternative at input ‘"{genPath}/{dataTag[0][‘name’]}"’ (, line 98)

Code:

`import json

filePath = 'E:\\tags.json'        
tagPaths = ["[default]"]
recursive = True
 
system.tag.exportTags(filePath, tagPaths, recursive)    

#Load JSON file
with open("tags.json") as f:
    data = json.load(f)
        
#Load XML template of GroupConfig from a text file
with open("template.txt", 'r') as f:
    TXML = f.read()
    
genPath = "" 

#Code to generate the path of the GroupConfig
dataX = data
while True:
    if dataX["tagType"] == "Folder":
        genPath += dataX["name"] + "/"
    
    try:
        dataX = dataX["tags"][0]
    except:
        break
genPath = genPath[:-1]
    
#Code to get the list of tags
dataTags = []
dataX = data
for lvl in genPath.split("/"):
    if dataX["name"] == lvl:
        if len(genPath.split("/")) == genPath.split("/").index(lvl) + 1:
            dataTags = dataX["tags"]
            break
        else:
            dataX = dataX["tags"][0]
    
tempDataTags = []        
for dataTag in dataTags:
    tempDataTags.append([dataTag, genPath])
dataTags = tempDataTags
            
#Checking for nested tags
while True:
    for dataTag in dataTags:
        if dataTag[0]["tagType"] == "Folder":
            for nDataTag in dataTag[0]["tags"]:
                dataTags.append([nDataTag, dataTag[1] + "/" + dataTag[0]["name"]])
            dataTags.remove(dataTag)
            newAdded = True
        else:
            newAdded = False
            
    if newAdded == False:
        break
    else:
        continue
            

#base XML structure with a vaiable '[[[TEMP]]]' to replace with the data
finalXML = "<Project><Groups>[[[TEMP]]]</Groups></Project>"

#EngHigh snippet, well be used to insert later in the code
engHigh = """<ItemConfig name="EngHigh" typeId="3">
          <Property name="TARGET_DATA_TYPE">5</Property>
          <Property name="DRIVING_TAG_PATH">[default]Enterprise 1/Site 1/Area 1/Line 1/Machine 1/Tag 1.EngHigh</Property>
          <Property name="TARGET_NAME">EngHigh</Property>
          <Property name="TARGET_TYPE">1</Property>
       </ItemConfig>"""


tempXML = "" #Temp vaiable to append groupConfig of each tags

#Looping Tags, generating GroupConfig for each tags, and appending to tempXML
for dataTag in dataTags:
    TEMP = TXML
    genPath = dataTag[1]
    
   
    try:
            #Taking historyProvider value to insert into DATA_SOURCE
        HP = dataTag[0]["historyProvider"]
        TEMP = TEMP.replace("[[[--SOURCE--]]]", HP)
   
        
        #if EngHigh is in the tags, it will be added to the XML
        try:
            EH = dataTag[0]["engHigh"]
            TEMP = TEMP.replace("[[[--ENGHIGH--]]]", engHigh)
        except:
            TEMP = TEMP.replace("[[[--ENGHIGH--]]]", "")
            
        #Inserting PATH to the XML
        TEMP = TEMP.replace("[[[--PATH--]]]", f"{genPath}/{dataTag[0]['name']}")
        TEMP = TEMP.replace("[[[--GCPATH--]]]", f"ContextualizedData/{genPath}")
        
        #Inserting NAME to the XML 'GroupConfig'
        TEMP = TEMP.replace("[[[--GCNAME--]]]", dataTag[0]["name"])
        
        #Appending to tempXML, generated GroupConfig of a tag
        tempXML += TEMP
        
    except KeyError:
        pass

#Adding all Generated GroupConfig to the finalXML
finalXML = finalXML.replace("[[[TEMP]]]", tempXML)

#Saving the XML String as XML file
with open("project.xml", "w") as f:
    f.write(finalXML)

Welcome!

Let’s start by editing your post to format the code portion. This will make it both easier to read and perhaps spot your issue more readily. :slight_smile:

  • At the bottom of your post there is a little pencil button to edit your post.
  • Highlight the code portion of your post
  • Click on the </> button in the editor.
4 Likes

At first looks though, keep in mind that the Jython version is 2.7, which does not support f-strings.

3 Likes

Make sure those quotes are ", not .
Also, as Jordan said, there are no fstrings in jython, you’ll need to use older formatting methods

"hello {user}".format(user="Rea_Berberi")
"hello {0}".format("Rea_Berberi")
"hello %s" % ("Rea_Berberi")

and the likes.

1 Like

They were so helpful. I have resolved it. Thank you!

Thank you for your answer it was so helpful. Just one more question : Is there a way to import an XML file from local env with transaction group configuration using script?

1 Like