Skip to main content
 首页 » 编程设计

python之python 中的 Split() 如果有条件必须跳过某些值,如何使用

2025年05月04日13EasonJim

我是 python 的新手,我想将一列中的数据(包括带有发行年份的电影名称)分隔到多列中,所以我找到了 split 函数。

数据按标题(年份)组织。

我在 python 中尝试的是:

movies['title'].str.split('(', 1, expand = True) 

以下情况发生异常:

City of Lost Children, The (Cité des enfants perdus, La) (1999)

City of Lost Children, The. Cité des enfants perdus, La) (1999)

我原以为只有 1999 年)转到第二列。

我需要你的帮助!

请您参考如下方法:

我投票赞成在这里使用 re.findall 模式 (.*?)\((\d{4})\):

input = """City of Lost Children, The (Cité des enfants perdus, La) (1999) 
           City of Lost Children, The. Cité des enfants perdus, La) (1999)""" 
 
matches = re.findall(r'\s*(.*?) \((\d{4})\)', input) 
print(matches) 

这打印:

[('City of Lost Children, The (Cité des enfants perdus, La)', '1999'), 
 ('City of Lost Children, The. Cité des enfants perdus, La)', '1999')]