def update_biaoqian(tag_list, title): if len(tag_list) == 0: print('没有标签,取标题作为标签') titless = re.sub('\s', ',', title) tag_list = title.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 # 以逗号作为分隔符,取二个 tag = tags.split(',')[-2:][0] else: tag_list = ''.join(tag_list) # 把空格,顿号,逗号,句号,问号,分好转换成英文逗号 tag_list = tag_list.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 # 以逗号作为分隔符,取二个 tag = tags.split(',')[-2:][0] return tag标签:空格,elif,tags,标签,list,replace,逗号,tag From: https://www.cnblogs.com/wolvies/p/17654029.html