python|英文标题格式化

2020-04-16

正确的英文标题格式

  •  首字母大写
  • 英文的介词等需要全部小写
  • 位于开头的单词,必须首字母大写;无论是否是介词等

python处理字符串的函数

capitalize()

# 利用 capitalize()函数,将字符串的首字母转化为大写,其余为小写
L1 = ['AdmIn','anny','LUCY','sandY','wILl']
def normallize(name):
    return name.capitalize()
L2 = list(map(normallize,L1))
print(L2)

upper()|lower()

#将字符串全部转化为大写or小写
title = 'benny'
print(title.upper())
print(upper(title))

title = 'BENNY'
print(title.lower())
print(lower(title))

最后的处理方法

import re
def isJudged(title):
    # 判断是否存在小写字母
    res = re.search('[a-z]', title)
    # print(res)
    if res:
        return True
    return False

# 核心功能,将首字母改为大写
def normallize(title):
    print('=======deal======')
    list = title.split(' ')
    new_list = []
    for index, item in enumerate(list):
        if index == 0:
            new_list.append(item.capitalize())
        else:
            if item.upper() in stop_words:
                new_list.append(item.lower())
            else:
                new_list.append(item.capitalize())
    res = ' '.join(new_list)
    return res

猜你喜欢

转载自www.cnblogs.com/bennyjane/p/12713404.html