Python分割單詞和轉(zhuǎn)換命名法的實現(xiàn)
分割單詞
將一個標識符分割成若干單詞存進列表,便于后續(xù)命名法的轉(zhuǎn)換
先引入正則表達式包
import re
至于如何分割單詞看個人喜好,如以常見分隔符 “ ”、“_”、“-”、“/”、“\” 去分割
re.split('[ _\-/\\\\]+', name)
還可以范圍再廣一點,拿除了數(shù)字和字母以外的所有字符去分割
re.split('[^0-9a-zA-Z]', name)那對于字母內(nèi)部怎么分割呢?
綜合考慮駝峰命名法、連續(xù)大寫的縮寫單詞等,筆者根據(jù)經(jīng)驗一般會采用這種策略,連續(xù)比較三個字符,滿足以下條件之一就分割:“小|大無”、“有|大小”、“小|大有”
- 是尾字符,是大寫,倒數(shù)第二個字符是小寫,在尾字符前分割,比如 'getA' 分割成 ['get','A']
- 是非首位的中間字符,是大寫,前后至少有一個是小寫,在該字符前分割,比如 'getJSONString' 分割成 ['get','JSON','String']
對于字母和數(shù)字結合的標識符,就比較難處理了
因為有的數(shù)字可以作為單詞開頭(比如 '3D'),有的又可以作為結尾(比如 'HTML5'),還有的字母數(shù)字交錯(比如 'm3u8'),暫未想到通用的分割的好辦法,根據(jù)個人需求實現(xiàn)就行了
綜合以上幾者的分割函數(shù)如下
def to_words(name):
? ? words = [] ? ? ? ? ? ? ? ? ?# 用于存儲單詞的列表
? ? word = '' ? ? ? ? ? ? ? ? ? # 用于存儲正在構建的單詞
? ? if(len(name) <= 1):
? ? ? ? words.append(name)
? ? ? ? return words
? ? # 按照常見分隔符進行分割
? ? # name_parts = re.split('[ _\-/\\\\]+', name)
? ? # 按照非數(shù)字字母字符進行分割
? ? name_parts = re.split('[^0-9a-zA-Z]', name)
? ? for part in name_parts:
? ? ? ? part_len = len(part) ? ? ? ?# 字符串的長度
? ? ? ? word = ''
? ? ? ? # 如果子串為空,繼續(xù)循環(huán)
? ? ? ? if not part:
? ? ? ? ? ? continue ??
? ? ? ? for index, char in enumerate(part):
? ? ? ? ? ? # “小|大無”
? ? ? ? ? ? if(index == part_len - 1):
? ? ? ? ? ? ? ? if(char.isupper() and part[index-1].islower()):
? ? ? ? ? ? ? ? ? ? if(word): words.append(word)
? ? ? ? ? ? ? ? ? ? words.append(char)
? ? ? ? ? ? ? ? ? ? word = ''
? ? ? ? ? ? ? ? ? ? continue
? ? ? ? ? ? # “有|大小”或“小|大有”
? ? ? ? ? ? elif(index != 0 and char.isupper()):
? ? ? ? ? ? ? ? if((part[index-1].islower() and part[index+1].isalpha()) or (part[index-1].isalpha() and part[index+1].islower())):
? ? ? ? ? ? ? ? ? ? if(word): words.append(word)
? ? ? ? ? ? ? ? ? ? word = ''
? ? ? ? ? ? word += char
? ? ? ? if(len(word) > 0): words.append(word)
? ? # 去除空單詞
? ? return [word for word in words if word != '']測試用例如下
print(to_words('IDCard')) # ['ID', 'Card']
print(to_words('getJSONObject')) # ['get', 'JSON', 'Object']
print(to_words('aaa@bbb.com')) # ['aaa', 'bbb', 'com']
print(to_words('D://documents/data.txt')) # ['D', 'documents', 'data', 'txt']
分割成全小寫單詞
def to_lower_words(name):
words = to_words(name)
return [word.lower() for word in words]
分割成全大寫單詞
def to_upper_words(name):
words = to_words(name)
return [word.upper() for word in words]
分割成首大寫、其余小寫單詞
def to_capital_words(name):
words = to_words(name)
return [word.capitalize() for word in words]
轉(zhuǎn)中劃線命名法
中劃線命名法,也叫烤肉串命名法(kebab case),如 'kebab-case'
- 字母全小寫
- 連字符連接
def to_kebab_case(name):
words = to_lower_words(name)
to_kebab_case = '-'.join(words)
return to_kebab_case
轉(zhuǎn)小蛇式命名法
小蛇式命名法,其實就是小寫下劃線命名法,也叫蛇式命名法(snake case),如 'snake_case'
- 字母全小寫
- 下劃線連接
def to_snake_case(name):
words = to_lower_words(name)
snake_case_name = '_'.join(words)
return snake_case_name
轉(zhuǎn)大蛇式命名法
大蛇式命名法,其實就是大寫下劃線命名法,也叫宏命名法(macro case),如 'MACRO_CASE'
- 字母全大寫
- 下劃線連接
def to_macro_case(name):
words = to_upper_words(name)
snake_case_name = '_'.join(words)
return snake_case_name
轉(zhuǎn)小駝峰命名法
小駝峰命名法,也叫駝峰命名法(camel case) ,如 'camelCase'
- 首單詞首字母小寫,后每個單詞首字母大寫
- 不使用連接符
def to_camel_case(name): ? ? words = to_words(name) ? ? camel_case_words = [] ? ? for word in words: ? ? ? ? if len(word) <= 1: ? ? ? ? ? ? camel_case_words.append(word.upper()) ? ? ? ? else: ? ? ? ? ? ? camel_case_words.append(word[0].upper() + word[1:]) ?? ?camel_case = ''.join(camel_case_words) ? ? if len(camel_case) <= 1: ? ? ? ? camel_case = camel_case.lower() ? ? else: ? ? ? ? camel_case = ''.join(camel_case[0].lower() + camel_case[1:]) ? ? return camel_case
轉(zhuǎn)大駝峰命名法
大駝峰命名法,也叫帕斯卡命名法(pascal case) ,如 'PascalCase'
- 每個單詞首字母大寫
- 不使用連接符
def to_pascal_case(name):
words = to_words(name)
pascal_case_words = []
for word in words:
if len(word) <= 1:
pascal_case_words.append(word.upper())
else:
pascal_case_words.append(word[0].upper() + word[1:])
pascal_case = ''.join(pascal_case_words)
return pascal_case
到此這篇關于Python分割單詞和轉(zhuǎn)換命名法的實現(xiàn)的文章就介紹到這了,更多相關Python分割單詞和轉(zhuǎn)換命名法內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
用Python計算三角函數(shù)之a(chǎn)tan()方法的使用
這篇文章主要介紹了用Python計算三角函數(shù)之a(chǎn)tan()方法的使用,是Python入門的基礎知識,需要的朋友可以參考下2015-05-05
python?memory_profiler庫生成器和迭代器內(nèi)存占用的時間分析
這篇文章主要介紹了python?memory_profiler庫生成器和迭代器內(nèi)存占用的時間分析,文章圍繞主題展開詳細的內(nèi)容介紹,感興趣的小伙伴可以參考一下2022-06-06
Cpython3.9源碼解析python中的大小整數(shù)
這篇文章主要介紹了Cpython3.9源碼解析python中的大小整數(shù),在CPython中,小整數(shù)對象池是一種優(yōu)化機制,用于減少對常用小整數(shù)的內(nèi)存分配和銷毀開銷,需要的朋友可以參考下2023-04-04
使用python3 實現(xiàn)插入數(shù)據(jù)到mysql
今天小編就為大家分享一篇使用python3 實現(xiàn)插入數(shù)據(jù)到mysql,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03

