1

I'm working with image metadata and able to extract a string that looks like this

Cube1[visible:true, mode:Normal]{r:Cube1.R, g:Cube1.G, b:Cube1.B, a:Cube1.A},
Ground[visible:true, mode:Normal]{r:Ground.R, g:Ground.G, b:Ground.B, a:Ground.A},
Cube3[visible:true, mode:Normal]{r:Cube3.R, g:Cube3.G, b:Cube3.B, a:Cube3.A},
Cube4[visible:true, mode:Normal]{r:Cube4.R, g:Cube4.G, b:Cube4.B, a:Cube4.A},
Sphere[visible:true, mode:Normal]{r:Sphere.R, g:Sphere.G, b:Sphere.B, a:Sphere.A},
OilTank[visible:true, mode:Normal]{r:OilTank.R, g:OilTank.G, b:OilTank.B, a:OilTank.A},
Cube2[visible:true, mode:Normal]{r:Cube2.R, g:Cube2.G, b:Cube2.B, a:Cube2.A}

I what convert that large mess to only the layer names. I also need for the order to stay the same. So, in this case it would be:

Cube1
Ground
Cube3
Cube4
Sphere
OilTank
Cube2

I've tried using "split" and "slice". I'm assuming there is a hierarchy here but I'm not sure where to go next.

6 Answers 6

1

If the data is indeed formated like that:

    import re
    i = [the listed string] 
    names = [j.strip('[') for j in re.findall("\w+\[\.*", i)]

Output:

['Cube1', 'Ground', 'Cube3', 'Cube4', 'Sphere', 'OilTank', 'Cube2']
Sign up to request clarification or add additional context in comments.

Comments

1

If you just need the left-most portion, I would use:

name, _ = line.split("[", 1)

If you need something more complex, I'd look into using regular expressions with the re module… Let me know and I can suggest something.

Comments

1
>>> mess = 'Cube1[visible:true, mode:Normal]{r:Cube1.R, g:Cube1.G, b:Cube1.B, a:Cube1.A},\nGround[visible:true, mode:Normal]{r:Ground.R, g:Ground.G, b:Ground.B, a:Ground.A},\nCube3[visible:true, mode:Normal]{r:Cube3.R, g:Cube3.G, b:Cube3.B, a:Cube3.A},\nCube4[visible:true, mode:Normal]{r:Cube4.R, g:Cube4.G, b:Cube4.B, a:Cube4.A},\nSphere[visible:true, mode:Normal]{r:Sphere.R, g:Sphere.G, b:Sphere.B, a:Sphere.A},\nOilTank[visible:true, mode:Normal]{r:OilTank.R, g:OilTank.G, b:OilTank.B, a:OilTank.A},\nCube2[visible:true, mode:Normal]{r:Cube2.R, g:Cube2.G, b:Cube2.B, a:Cube2.A}'
>>> names = "\n".join(line.split("[", 1)[0] for line in mess.split("\n"))
>>> print names
Cube1
Ground
Cube3
Cube4
Sphere
OilTank
Cube2

Comments

0

I don't know a lot about python, but my thoughts in terms of logic would be this:

  1. Split on the comma character
  2. Loop on the resulting array and cut off everything after the first '[' using substring(indexOf) or similar python manipulation.
  3. Then loop though the array again to concatenate the strings back together.

Sorry I don't know the specific commands for doing this. Hope it helps!

Comments

0

Regexes are unecessary, assuming that really is the exact format of your data.

[i.split('[', 1)[0] for i in lst]

Comments

0

With string split:

names = [ x.split('[')[0] for x in your_text.split('\n') ]

With regular expressions:

import re
names = re.findall(r'^\w+', your_text, re.MULTILINE)

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.