【发布时间】:2019-05-20 01:53:52
【问题描述】:
假设这个字符串:
[aaa ] some text here [bbbb3 ] some other text here [cc ] more text
我想要一个像这样的键值对:
Key Value
aaa some text here
bbbb3 some other text here
cc more text
或者像这样的 pandas DataFrame
aaa | bbbb3 |cc
-------------------------------------------------
some text here | some other text here | more text
next line | ..... | .....
我尝试了一个正则表达式,例如:r'\[(.{6})\]\s(.*?)\s\[',但这不起作用。
【问题讨论】:
-
试试
r'\[([^][]*?)\s*]\s*(.*?)(?=\s*\[|$)',见this demo。 -
这是要进入 pandas DataFrame 吗?
-
@K.Madden:这不是在回答我的问题。我需要正则表达式来捕获或拆分我的字符串。
-
@PatrickArtner:抱歉打错了,更新示例
标签: python regex python-3.x pandas