我在以下文本中遇到了分隔符的问题。我想在文本之间以及接下来的三个数字之间放置“|”。问题是有时数字实际上是破折号,表示0(即--而不是0)。
这将输出:
text = """
A line of text 85 25,653 -75,321
Another - line 5,432 (5,353) --
Another one 23 -0- 86
One -- more -- -0- 34 25
"""
目前为止,我能够通过以下代码获取大多数'|'分隔符:
text = re.sub(r'\s[(](\d)', '|(\\1', text)
text = re.sub(r'\s(\d)', '|\\1', text)
text = re.sub(r'\s-(\d)', '|-\\1', text)
这将输出:
A line of text |85 |25,653 |-75,321
Another - line |5,432 |(5,353) --
Another one |23 |-0- |86
One -- more --|-0-|34|25
然而,在第4行的第二个--之前,我希望有另一个分隔符,而不是在One和--之间放置分隔符。有没有办法在Python中实现这个要求?
我希望最终输出为:
A line of text |85 |25,653 |-75,321
Another - line |5,432 |(5,353) |--
Another one |23 |-0- |86
One -- more |--|-0-|34|25