Python 字符串剥离和分割
我正在处理图像元数据,并能够提取一个看起来像这样的字符串,
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}
我将大混乱转换为仅图层名称。我还需要订单保持不变。所以,在这种情况下,它是:
Cube1
Ground
Cube3
Cube4
Sphere
OilTank
Cube2
我尝试使用“split”和“slice”。我假设这里有一个层次结构,但我不确定下一步该去哪里。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
如果数据确实是这样的格式:
输出:
If the data is indeed formated like that:
Output:
如果您只需要最左边的部分,我会使用:
如果您需要更复杂的内容,我会考虑使用正则表达式与
re
模块...让我知道,我可以提出一些建议。If you just need the left-most portion, I would use:
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.我对 python 不太了解,但我在逻辑方面的想法是这样的:
抱歉,我不知道执行此操作的具体命令。希望有帮助!
I don't know a lot about python, but my thoughts in terms of logic would be this:
Sorry I don't know the specific commands for doing this. Hope it helps!
正则表达式是不必要的,假设这确实是您的数据的确切格式。
Regexes are unecessary, assuming that really is the exact format of your data.
使用字符串分割:
使用正则表达式:
With string split:
With regular expressions: