首页 > 解决方案 > 在字符串的特定位置插入字符

问题描述

我有以下问题。我想在特定列的以下字符串中的每个“)”之后插入“,”。

The Bahamas (All locations)Bermuda (All locatins)Romania (Cluj)Bulgaria (All locations)

这个想法是我在同一个表的同一列的多行上都有这种类型的字符串。请在这个问题上帮助我。

我希望有:

The Bahamas (All locations),Bermuda (All locatins),Romania (Cluj),Bulgaria (All locations), 

为了以我需要的信息的方式进行拆分。

标签: pythonpython-3.xinsertjupyter-notebook

解决方案


使用 str.replace():

In [1]: s1 = 'The Bahamas (All locations)Bermuda (All locatins)Romania (Cluj)Bulgaria (All locations)'

In [2]: s2 = s1.replace(')', '),')

In [3]: s2
Out[3]: 'The Bahamas (All locations),Bermuda (All locatins),Romania (Cluj),Bulgaria (All locations),'

推荐阅读