欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python分割單詞和轉(zhuǎn)換命名法的實(shí)現(xiàn)

 更新時(shí)間:2023年03月15日 16:17:37   作者:夢(mèng)未  
本文主要介紹了Python分割單詞和轉(zhuǎn)換命名法的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

分割單詞

將一個(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)文章

最新評(píng)論