前置参数批量修改

在架构调整时,我们的posts前置变量总是在不断变化,而同时这些posts又已经在生产环境之下,受过修改。因此,无损迁移posts,成为了本文的主题。

关于文件内容读取和修改,我们主要使用python

1
2
3
4
5
6
7
8

import re, os

path1 = r'C:\Users\Bachelor\Desktop\ABC\our\content\dailybrief'

old_str='---((.|\n|\r)*)<!--more-->' #包括摘要
#old_str='---((.|\n|\r)*)---' 
files = os.listdir(path1)
 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68

for j in range(0, 4870):
    file = files[j]
    file = file.rstrip('.html')
    file = file.rstrip('.md')
    year = file.split('-')[0]
    month = file.split('-')[1]
    date= file.split('-')[2][0:2]
    fdate=year+'-'+month+'-'+date
    print(j,year,file)

    c = month.replace('01', 'January')
    c = c.replace('06','June' )
    c = c.replace('03','March' )
    c = c.replace('05','May' )
    c = c.replace('11','November' )
    c = c.replace('09','September')
    c = c.replace('04','April' )
    c = c.replace('08','August' )
    c = c.replace('12','December' )
    c = c.replace('02','February' )
    c = c.replace('07', 'July')
    c = c.replace('10','October')

    c = month.replace('01', 'January')
    c = c.replace('06','June' )
    c = c.replace('03','March' )
    c = c.replace('05','May' )
    c = c.replace('11','November' )
    c = c.replace('09','September')
    c = c.replace('04','April' )
    c = c.replace('08','August' )
    c = c.replace('12','December' )
    c = c.replace('02','February' )
    c = c.replace('07', 'July')
    c = c.replace('10','October')


    imonth = month
    if month == '10':
        print(month)
    else:
        month = month.replace('0', '')
        print(month)

    idate=date
    if date[0] == '0':
        date = date.replace('0', '')

    title = 'Intelligence History on ' + c + ' ' + date
    a ="---\n"
    a=''.join([a, ("title: %s\n" %file)])
    a=''.join([a,("date: %s\n" %fdate )])
    a=''.join([a,("author: CIA \n")])
    a = ''.join([a, ("authorLink: \"/authors/cia\"\n")])
    a=''.join([a,("timestamp: \n")])
    a=''.join([a,("- %s\n" %year)])
    a=''.join([a,("- %s-%s\n" % (year,imonth ))])
    a=''.join([a,("multitype: \n")])
    a=''.join([a,("- cia\n")])
    a=''.join([a,("tags: \n")])
    a=''.join([a,("- dailybrief\n")])
    a=''.join([a,("featuredImagePreview: \'/agency/CIA.png\'\n")])
    a=''.join([a,("---\n\n\n")])
    a = ''.join([a, ("Presidential Daily Brief On %s %s %s\n\n" % (year, c, date))])
    a=''.join([a,("此情报简报于%s年%s月%s日被递交给时任美国总统\n\n" %(year,month,date))])

    a=''.join([a,("<!--more-->\n\n")])
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16

    with open(path1 + "/" + file + '.md', mode='r+', encoding="utf-8") as f1:

        filec = f1.read()
        # print(filec, 'what')
        new_str = a
        b=re.sub(old_str, new_str, filec)
        print(j,file,'this______________________')
        # 设置位置到页面顶部插入数据
        f1.seek(0)
        # 在文件中写入替换数据
        f1.write(b)
        # 截断文件大小
        f1.truncate()

        f1.close()

Communication from Bachelor.