取标签列表的最后两位,并去除最后一个逗号,没有标签就用标题

发布时间 2023-11-13 09:20:03作者: 布都御魂
def update_biaoqian(tag_list, title):
    if tag_list == [''] or tag_list == []:
        print('没有标签,取标题作为标签')
        titless = re.sub('\s', ',', title)
        tag_list = title.replace('', ',').replace('', ',').replace('', ',').replace('', ',').replace(' ', ',').replace('', ',').replace('|', ',').replace('/', ',')
        # 把相邻的逗号简化成一个
        tags = re.sub(r"(,)\1+", r"\1", tag_list)
        # 开头是逗号,从第二个开始取值
        if tags[0] == ',' and tags[-1] != ',':
            tags = tags[1:]
        # 结尾时句号,从倒数第二位取值
        elif tags[0] != ',' and tags[-1] == ',':
            tags = tags[:-1]

        elif tags[0] == ',' and tags[-1] == ',':
            tags = tags[1:-1]
        elif tags[0] != ',' and tags[-1] != ',':
            tags = tags
        # 以逗号作为分隔符,取二个
        tags_list = tags.split(',')[-2:]
        tag = ''
        for tags in tags_list:
            tag += tags + ','
        tag = tag[:-1]
    else:
        tag_list = ''.join(tag_list)
        # 把空格,顿号,逗号,句号,问号,分好转换成英文逗号
        tag_list = tag_list.replace('', ',').replace('', ',').replace('', ',').replace('', ',').replace(' ', ',').replace('', ',').replace('|', ',')
        # 把相邻的逗号简化成一个
        tags = re.sub(r"(,)\1+", r"\1", tag_list)
        # 开头是逗号,从第二个开始取值
        if tags[0] == ',' and tags[-1] != ',':
            tags = tags[1:]
        # 结尾时句号,从倒数第二位取值
        elif tags[0] != ',' and tags[-1] == ',':
            tags = tags[:-1]

        elif tags[0] == ',' and tags[-1] == ',':
            tags = tags[1:-1]
        elif tags[0] != ',' and tags[-1] != ',':
            tags = tags
        # 以逗号作为分隔符,取二个
        tags_list = tags.split(',')[-2:]
        tag = ''
        for tags in tags_list:
            tag += tags + ','
        tag = tag[:-1]
    return tag