Python - 如何删除多个空格

3

如下所示,有大量空格,从行首、行尾到行中都有。我正在尝试从中间删除这些额外的空格。以下是我的尝试,但我一直收到错误提示:

testdata = [{'col1': ' Sea Ice   Prediction     Network .    '},
     {'col1': ' Movies, Ratings, ....        etc.'},
     {'col1': 'Iceland, Greenland, Mountains  '},
     {'col1': ' My test file'}]
df = pd.DataFrame(testdata)

' '.join(testdata['col1'].split()) #Error: list indices must be integers or slices, not str

df['col1'].str.lstrip() #list indices must be integers or slices, not str
df['col1'].str.rstrip() #list indices must be integers or slices, not str

 #removes start and end, but not ideal to remove one line at a time. 
' Sea Ice     Prediction Network .    '.lstrip()
' Sea Ice     Prediction Network .    '.rstrip()

我该怎么移除这个?谢谢!
Clean Output: 

'Sea Ice Prediction Network .'
'Movies, Ratings, .... etc.'
'Iceland, Greenland, Mountains '
'My test file'

当你有一个DataFrame时,为什么要索引到testdata中? - ayhan
2个回答

7

使用replace

df.replace({' +':' '},regex=True)
Out[348]: 
                             col1
0   Sea Ice Prediction Network . 
1      Movies, Ratings, .... etc.
2  Iceland, Greenland, Mountains 
3                    My test file

2
你可以使用re模块将字符串中的任何空格替换为单个空格,然后从开头和结尾删除任何内容。
re.sub('\s+', ' ', ' Sea Ice   Prediction     Network .    ').strip()
'Sea Ice Prediction Network .'

那个点号前的空格有影响吗?

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接