ผลลัพธ์สำหรับการแยก Camel Case Strings เป็น Series คือ
enter the sring: pandasSeriesDataFrame Series is: 0 pandas 1 Series 2 Data 3 Frame dtype: object
เพื่อแก้ปัญหานี้ เราจะทำตามขั้นตอนด้านล่าง −
Solution
กำหนดฟังก์ชันที่รับ Input String
ตั้งค่าตัวแปร Result ด้วยเงื่อนไข Input ไม่ใช่ Lowercase และไม่ใช่ Uppercase และไม่มี '_' ใน Input String คือ ด้านล่าง
result = (s != s.lower() and s != s.upper() and "_" not in s)
ตั้งเงื่อนไข If ถ้า Result เป็น True ให้ใช้ re.findall หา Pattern Camel Case และแปลง Input String เป็น Series คือ ด้านล่าง
pd.Series(re.findall(r'[A-Za-z](?:[a-z]+|[A-Z]*(?=[A-Z]|$))', s))
หากเงื่อนไขเป็น False ให้พิมพ์ว่า Input ไม่ใช่รูปแบบ Camel Case
ตัวอย่าง
ตอนนี้ ลองดู Implementation เพื่อเข้าใจดีขึ้น −
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