我有一串文字,如下所示:
' 19,301 14,856 18,554'
Run Code Online (Sandbox Code Playgroud)
空间在哪里.
我正在尝试将其拆分为空白区域,但我需要将所有空白区域保留为新列表中的项目.像这样:
[' ', '19,301',' ', '14,856', ' ', '18,554']
Run Code Online (Sandbox Code Playgroud)
我一直在使用以下代码:
re.split(r'( +)(?=[0-9])', item)
Run Code Online (Sandbox Code Playgroud)
它返回:
['', ' ', '19,301', ' ', '14,856', ' ', '18,554']
Run Code Online (Sandbox Code Playgroud)
请注意,它总是将空元素添加到列表的开头.删除它很容易,但我真的很想了解这里发生了什么,所以我可以得到代码来一致地处理事情.谢谢.
使用Python,我有以下字符串:
['taxes............................. .7 21.4 (6.2)','regulatory and other matters..................$ 39.9 61.5 41.1','Producer contract reformation cost recoveries............................ DASH 26.3 28.3']
Run Code Online (Sandbox Code Playgroud)
我需要用空格替换每个点,而不是数字中的句点.所以结果应该是这样的:
['taxes .7 21.4 (6.2)','regulatory and other matters $ 39.9 61.5 41.1','Producer contract reformation cost recoveries DASH 26.3 28.3']
Run Code Online (Sandbox Code Playgroud)
我尝试过以下方法:
dots=re.compile('(\.{2,})(\s*?[\d\(\$]|\s*?DASH|\s*.)')
newlist=[]
for each in list:
newline=dots.sub(r'\2'.replace('.',' '),each)
newdoc.append(newline)
Run Code Online (Sandbox Code Playgroud)
但是,此代码不保留空白区域.谢谢!