0%

文章分类和标签统计

有时候不知道博客文章该给什么分类和标签,所以写了个统计已有文章的分类和标签及数量的小工具。

注意,仅支持 yaml 格式的开头,类似这样:

1
2
3
4
5
6
7
8
---
title: 文章分类和标签统计
date: 2022-06-04 13:27:55
updated: 2022-06-04 13:27:55
categories:
- [小工具]
tags: [Python]
---

要求

  • categories 可以一行或多行,一行也必须带 [] 括号,如:categories: [小工具],多行格式类似上面展示的样式
  • tags: 必须一行,必须带括号 []

使用方法

1
python Category_Tag.py > out.txt

其中 Category_Tag.py 是保存了下面展示代码的脚本。

代码

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
import os
from collections import Counter

if __name__ == '__main__':
cate, tags = Counter(), Counter()
for dirpath, dirnames, filenames in os.walk("./source/_posts"):
for filename in filenames:
if filename.endswith('.md'):
st = list()
with open(os.path.join(dirpath, filename), 'rt+', encoding='utf-8') as f:
state = 0
for line in f:
if state == 1 and '---' in line:
break
if "categories" in line:
state = 1
if state == 1:
st.append(line)
*categories, tag = st
if len(categories) == 1:
category = [categories[0].split(':')[1].strip()]
else:
category = map((lambda x: x.strip()[3:-1]), categories[1:])
tag = tag.strip()[7:-1].split(',')
for c in category:
cate[c] += 1
for t in tag:
tags[t] += 1
print("Categories:")
for k, v in cate.items():
print(f"- {k}: {v}")
print()
print("Tags:")
for k, v in tags.items():
print(f"- {k}: {v}")

输出

输出样式类似这样:

1
2
3
4
5
6
7
Categories:
- about: 1
- LeetCode: 25

Tags:
- : 3
- LeetCode: 24
--- ♥ end ♥ ---

欢迎关注我呀~