背景

文章链接选择 :title:filename 之类的,但是会有中文,不使用中文,又对查找不太方便。

思路

使用 python 将中文转为拼音,然后读取文件内容并进行替换。

中文转拼音示例

1
2
3
4
5
6
7
8
from xpinyin import Pinyin

p = Pinyin()

result = p.get_pinyin('我爱你中国')

print(result)
# wo-ai-ni-zhong-guo

替换文件内容

遍历目录

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# dir.py
import os
import file_replace


# 遍历文件夹
def walk_file(file):
    for root, dirs, files in os.walk(file):

        # root 表示当前正在访问的文件夹路径
        # dirs 表示该文件夹下的子目录名list
        # files 表示该文件夹下的文件list

        # 遍历文件
        for f in files:
            file_path = os.path.join(root, f)
            print(file_path)
            file_replace.alter(file_path)

        # 遍历所有的文件夹
        # for d in dirs:
        # print(os.path.join(root, d))


def main():
    walk_file("path")


if __name__ == '__main__':
    main()

内容替换

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# file_replace.py
import re
from xpinyin import Pinyin


def alter(file, old_str='', new_str=''):
    """
    替换文件中的字符串
    :param file:文件名
    :param old_str:就字符串
    :param new_str:新字符串
    :return:
    """
    p = Pinyin()
    file_data = ""
    title_pattern = 'title: ?"(.*)?"'
    slug_pattern = 'slug:(.*)?'
    with open(file, "r", encoding="utf-8") as f:
        for line in f:
            match_slug = re.match(slug_pattern, line, re.M | re.I)
            print(match_slug)
            if match_slug:
                slug = match_slug.group(1)
                if slug:
                    line = ''

            match_title = re.match(title_pattern, line, re.M | re.I)
            if match_title:
                title = match_title.group(1)
                if title:
                    title_pinyin = p.get_pinyin(title, ' ')
                    print(title_pinyin)
                    title_pinyin = re.sub("[^a-zA-Z-0-9.]+", " ", title_pinyin)
                    title_pinyin = re.sub(" +", " ", title_pinyin)
                    print(title_pinyin)
                    new_str = 'slug: "' + title_pinyin + '"\n'
                    line = new_str + line

            file_data += line
    with open(file, "w", encoding="utf-8") as f:
        f.write(file_data)

# alter("path", "old_str", "new_str")