gpt4 book ai didi

python - 将大写和小写字符串的字符串列拆分为两个单独的列 Pyspark/Python/Sql?

转载 作者:行者123 更新时间:2023-12-04 13:08:12 24 4
gpt4 key购买 nike

我在点击列中有以下数据:

    MEM-BEN-BTN-CLK-entertainment-audible
MEM-BEN-LOC-MODAL-LOCATION-INPUT-Birmingham, AL, USA
MEM-BEN-BTN-CLK-entertainment-games
MEM-BEN-BTN-CLK-healthandwellness-love-and-meaning-after-50
MEM-BEN-BTN-LRN-learn-more-aarp-travel-center-powered-by-expedia-10083
MEM-BEN-BTN-LRN-learn-more-embassy-suites-by-hilton-1019

我想将 Click 列拆分为两列 Click_Upper 和 Click_lower

Click_Upper(包含所有大写字符)

    MEM-BEN-BTN-CLK
MEM-BEN-LOC-MODAL-LOCATION-INPUT
MEM-BEN-BTN-CLK
MEM-BEN-BTN-CLK
MEM-BEN-BTN-LRN
MEM-BEN-BTN-LRN

Click_lower(包含所有小写字符)

    entertainment-audible
Birmingham, AL, USA
entertainment-games
healthandwellness-love-and-meaning-after-50
learn-more-aarp-travel-center-powered-by-expedia-10083
learn-more-embassy-suites-by-hilton-1019

我试图使用 split() 函数,但有多个定界符 (-) 并且字符串的长度各不相同,因此代码对我不起作用。也试过 re 但它破坏了字符串。

如果我能在这方面得到任何指导或帮助,我将不胜感激。

最佳答案

我使用正则表达式语句来拆分字符串。您可以使用 re.group(x) 方法访问这两个组。以下是更多信息:https://docs.python.org/3/library/re.html

import re

strings = ["MEM-BEN-BTN-CLK-entertainment-audible",
"MEM-BEN-LOC-MODAL-LOCATION-INPUT-Birmingham, AL, USA",
"MEM-BEN-BTN-CLK-entertainment-games",
"MEM-BEN-BTN-CLK-healthandwellness-love-and-meaning-after-50",
"MEM-BEN-BTN-LRN-learn-more-aarp-travel-center-powered-by-expedia-10083",
"MEM-BEN-BTN-LRN-learn-more-embassy-suites-by-hilton-1019"]

regex = "(?P<Click_Upper>[A-Z\-]+)-(?P<Click_Lower>.*)"

for string in strings:
print(re.match(regex,string).groups())

这是输出:

('MEM-BEN-BTN-CLK', 'entertainment-audible')
('MEM-BEN-LOC-MODAL-LOCATION-INPUT', 'Birmingham, AL, USA')
('MEM-BEN-BTN-CLK', 'entertainment-games')
('MEM-BEN-BTN-CLK', 'healthandwellness-love-and-meaning-after-50')
('MEM-BEN-BTN-LRN', 'learn-more-aarp-travel-center-powered-by-expedia-10083')
('MEM-BEN-BTN-LRN', 'learn-more-embassy-suites-by-hilton-1019')

关于python - 将大写和小写字符串的字符串列拆分为两个单独的列 Pyspark/Python/Sql?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68384082/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com