Hi,
I am having a string like below. What I need to do is to process the string in such a way that I could get the output in a format like below.The problem I am facing here is that some elements have commas(,) within itself , so it is difficult to process it using the split() command. Can anyone suggest me a mechanism to process a string like below to a output (which I have shown).


String:
"returns=int", "operationName=doComplexStuff", var1="integer", var2="(a="integer", b="integer")", var3="(a="string", b="integer")"


output:
returns --- int
operationName --- doComplexStuff
var1 --- integer
var2 --- a -- integer
var2 --- b -- integer
var3 --- a -- string
var3 --- b -- integer

You need to get involved in parsing, here is example:

data = '''\
"returns=int",
"operationName=doComplexStuff",
var1="integer",
var2="(a="integer", b="integer")",
var3="(a="string", b="integer")"
'''

q = data.split(',')
m = []
t = ""
con = False
for k in range(len(q)):
    s = q[k].strip()

    # parse for () pairs and concatenate
    # before appending (set con flag)
    if '(' in s:
        con = True
    if con == True and ')' not in s:
        t += s + ','
    if ')' in s:
        t += s
        m.append(t)
        t = ""
        con = False
    elif con == False:
        m.append(s)

# show result
for w in m:
    print w

"""
my result -->
"returns=int"
"operationName=doComplexStuff"
var1="integer"
var2="(a="integer",b="integer")"
var3="(a="string",b="integer")"
"""

There are parsing modules available for Python.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.