用Python编写一个程序,以验证用户的驼峰案例字符串,拆分驼峰案例,并将其存储在新系列中

将驼峰式案例字符串拆分为系列的结果为,

enter the sring:
pandasSeriesDataFrame
Series is:
0    pandas
1    Series
2    Data
3    Frame
dtype: object

为了解决这个问题,我们将遵循以下步骤-

解决方案

  • 定义一个接受输入字符串的函数

  • 使用输入条件设置结果变量不是小写和大写,并且输入字符串中不能包含“ _”。它的定义如下

result = (s != s.lower() and s != s.upper() and "_" not in s)

  • 设置if条件以检查结果是否为true的applyre.findall方法查找驼峰式模式并将输入字符串转换为序列。它的定义如下

pd.Series(re.findall(r'[A-Za-z](?:[a-z]+|[A-Z]*(?=[A-Z]|$))', s)

  • 如果条件变为假,则打印输入的格式不是驼峰格式。

例子

现在,让我们检查其实现以更好地了解-

import pandas as pd
import re
def camelCase(s):
   result = (s != s.lower() and s != s.upper() and "_" not in s)
   if(result==True):
      series = pd.Series(re.findall(r'[A-Za-z](?:[a-z]+|[A-Z]*(?=[AZ]|$))', s))
      print(series)
   else:
      print("input is not in came case format")
s = input("enter the sring")
camelCase(s)

输出

enter the sring:
pandasSeriesDataFrame
Series is:
0    pandas
1    Series
2    Data
3    Frame
dtype: object
enter the sring: pandasseries
input is not in came case format