Python分割單詞和轉(zhuǎn)換命名法的實(shí)現(xiàn)
分割單詞
將一個(gè)標(biāo)識(shí)符分割成若干單詞存進(jìn)列表,便于后續(xù)命名法的轉(zhuǎn)換
先引入正則表達(dá)式包
import re
至于如何分割單詞看個(gè)人喜好,如以常見(jiàn)分隔符 “ ”、“_”、“-”、“/”、“\” 去分割
re.split('[ _\-/\\\\]+', name)
還可以范圍再?gòu)V一點(diǎn),拿除了數(shù)字和字母以外的所有字符去分割
re.split('[^0-9a-zA-Z]', name)
那對(duì)于字母內(nèi)部怎么分割呢?
綜合考慮駝峰命名法、連續(xù)大寫(xiě)的縮寫(xiě)單詞等,筆者根據(jù)經(jīng)驗(yàn)一般會(huì)采用這種策略,連續(xù)比較三個(gè)字符,滿(mǎn)足以下條件之一就分割:“小|大無(wú)”、“有|大小”、“小|大有”
- 是尾字符,是大寫(xiě),倒數(shù)第二個(gè)字符是小寫(xiě),在尾字符前分割,比如 'getA' 分割成 ['get','A']
- 是非首位的中間字符,是大寫(xiě),前后至少有一個(gè)是小寫(xiě),在該字符前分割,比如 'getJSONString' 分割成 ['get','JSON','String']
對(duì)于字母和數(shù)字結(jié)合的標(biāo)識(shí)符,就比較難處理了
因?yàn)橛械臄?shù)字可以作為單詞開(kāi)頭(比如 '3D'),有的又可以作為結(jié)尾(比如 'HTML5'),還有的字母數(shù)字交錯(cuò)(比如 'm3u8'),暫未想到通用的分割的好辦法,根據(jù)個(gè)人需求實(shí)現(xiàn)就行了
綜合以上幾者的分割函數(shù)如下
def to_words(name): ? ? words = [] ? ? ? ? ? ? ? ? ?# 用于存儲(chǔ)單詞的列表 ? ? word = '' ? ? ? ? ? ? ? ? ? # 用于存儲(chǔ)正在構(gòu)建的單詞 ? ? if(len(name) <= 1): ? ? ? ? words.append(name) ? ? ? ? return words ? ? # 按照常見(jiàn)分隔符進(jìn)行分割 ? ? # name_parts = re.split('[ _\-/\\\\]+', name) ? ? # 按照非數(shù)字字母字符進(jìn)行分割 ? ? name_parts = re.split('[^0-9a-zA-Z]', name) ? ? for part in name_parts: ? ? ? ? part_len = len(part) ? ? ? ?# 字符串的長(zhǎng)度 ? ? ? ? word = '' ? ? ? ? # 如果子串為空,繼續(xù)循環(huán) ? ? ? ? if not part: ? ? ? ? ? ? continue ?? ? ? ? ? for index, char in enumerate(part): ? ? ? ? ? ? # “小|大無(wú)” ? ? ? ? ? ? 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 != '']
測(cè)試用例如下
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']
分割成全小寫(xiě)單詞
def to_lower_words(name): words = to_words(name) return [word.lower() for word in words]
分割成全大寫(xiě)單詞
def to_upper_words(name): words = to_words(name) return [word.upper() for word in words]
分割成首大寫(xiě)、其余小寫(xiě)單詞
def to_capital_words(name): words = to_words(name) return [word.capitalize() for word in words]
轉(zhuǎn)中劃線(xiàn)命名法
中劃線(xiàn)命名法,也叫烤肉串命名法(kebab case),如 'kebab-case'
- 字母全小寫(xiě)
- 連字符連接
def to_kebab_case(name): words = to_lower_words(name) to_kebab_case = '-'.join(words) return to_kebab_case
轉(zhuǎn)小蛇式命名法
小蛇式命名法,其實(shí)就是小寫(xiě)下劃線(xiàn)命名法,也叫蛇式命名法(snake case),如 'snake_case'
- 字母全小寫(xiě)
- 下劃線(xiàn)連接
def to_snake_case(name): words = to_lower_words(name) snake_case_name = '_'.join(words) return snake_case_name
轉(zhuǎn)大蛇式命名法
大蛇式命名法,其實(shí)就是大寫(xiě)下劃線(xiàn)命名法,也叫宏命名法(macro case),如 'MACRO_CASE'
- 字母全大寫(xiě)
- 下劃線(xiàn)連接
def to_macro_case(name): words = to_upper_words(name) snake_case_name = '_'.join(words) return snake_case_name
轉(zhuǎn)小駝峰命名法
小駝峰命名法,也叫駝峰命名法(camel case) ,如 'camelCase'
- 首單詞首字母小寫(xiě),后每個(gè)單詞首字母大寫(xiě)
- 不使用連接符
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'
- 每個(gè)單詞首字母大寫(xiě)
- 不使用連接符
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
到此這篇關(guān)于Python分割單詞和轉(zhuǎn)換命名法的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)Python分割單詞和轉(zhuǎn)換命名法內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python編寫(xiě)圖書(shū)管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了python編寫(xiě)圖書(shū)管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03關(guān)于Python參數(shù)解析器argparse的應(yīng)用場(chǎng)景
這篇文章主要介紹了關(guān)于Python參數(shù)解析器argparse的應(yīng)用場(chǎng)景,argparse 模塊使編寫(xiě)用戶(hù)友好的命令行界面變得容易,程序定義了所需的參數(shù),而 argparse 將找出如何從 sys.argv 中解析這些參數(shù),需要的朋友可以參考下2023-08-08Python利用Turtle繪畫(huà)簡(jiǎn)單圖形
這篇文章主要介紹了Python利用Turtle繪畫(huà)簡(jiǎn)單圖形,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的朋友可以參考一下2022-07-07python自動(dòng)獲取微信公眾號(hào)最新文章的實(shí)現(xiàn)代碼
這篇文章主要介紹了python自動(dòng)獲取微信公眾號(hào)最新文章,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-07-07Python實(shí)現(xiàn)圖片轉(zhuǎn)字符畫(huà)的代碼實(shí)例
今天小編就為大家分享一篇關(guān)于Python實(shí)現(xiàn)圖片轉(zhuǎn)字符畫(huà)的代碼實(shí)例,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-02-02Python使用sklearn庫(kù)實(shí)現(xiàn)的各種分類(lèi)算法簡(jiǎn)單應(yīng)用小結(jié)
這篇文章主要介紹了Python使用sklearn庫(kù)實(shí)現(xiàn)的各種分類(lèi)算法,結(jié)合實(shí)例形式分析了Python使用sklearn庫(kù)實(shí)現(xiàn)的KNN、SVM、LR、決策樹(shù)、隨機(jī)森林等算法實(shí)現(xiàn)技巧,需要的朋友可以參考下2019-07-07pyqt5讓圖片自適應(yīng)QLabel大小上以及移除已顯示的圖片方法
今天小編就為大家分享一篇pyqt5讓圖片自適應(yīng)QLabel大小上以及移除已顯示的圖片方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-06-06Python實(shí)現(xiàn)將一個(gè)大文件按段落分隔為多個(gè)小文件的簡(jiǎn)單操作方法
這篇文章主要介紹了Python實(shí)現(xiàn)將一個(gè)大文件按段落分隔為多個(gè)小文件的簡(jiǎn)單操作方法,涉及Python針對(duì)文件的讀取、遍歷、轉(zhuǎn)換、寫(xiě)入等相關(guān)操作技巧,需要的朋友可以參考下2017-04-04Python小實(shí)例混合使用turtle和tkinter讓小海龜互動(dòng)起來(lái)
Tkinter模塊("Tk 接口")是Python的標(biāo)準(zhǔn)Tk GUI工具包的接口.Tk和Tkinter可以在大多數(shù)的Unix平臺(tái)下使用,同樣可以應(yīng)用在Windows和Macintosh系統(tǒng)里.Tk8.0的后續(xù)版本可以實(shí)現(xiàn)本地窗口風(fēng)格,并良好地運(yùn)行在絕大多數(shù)平臺(tái)中2021-10-10