Python 字符串剥离和分割

发布于 2024-11-29 06:41:34 字数 802 浏览 0 评论 0原文

我正在处理图像元数据,并能够提取一个看起来像这样的字符串,

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

小兔几 2024-12-06 06:41:34

如果数据确实是这样的格式:

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

输出:

['Cube1', 'Ground', 'Cube3', 'Cube4', 'Sphere', 'OilTank', 'Cube2']

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']
萧瑟寒风 2024-12-06 06:41:34

如果您只需要最左边的部分,我会使用:

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

如果您需要更复杂的内容,我会考虑使用正则表达式与 re 模块...让我知道,我可以提出一些建议。

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.

您的好友蓝忘机已上羡 2024-12-06 06:41:34
>>> 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
>>> 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
二智少女 2024-12-06 06:41:34

我对 python 不太了解,但我在逻辑方面的想法是这样的:

  1. 在逗号字符上分割
  2. 在结果数组上循环并使用 substring(indexOf) 或类似的 python 操作切断第一个 '[' 之后的所有内容。
  3. 然后再次循环数组以将字符串重新连接在一起。

抱歉,我不知道执行此操作的具体命令。希望有帮助!

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!

醉殇 2024-12-06 06:41:34

正则表达式是不必要的,假设这确实是您的数据的确切格式。

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

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

[i.split('[', 1)[0] for i in lst]
最美的太阳 2024-12-06 06:41:34

使用字符串分割:

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

使用正则表达式:

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

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文