【备忘】替换文件中特定字符串(缺re库时)

本文介绍了一种在受限环境中(如无relibrary可用的服务器),使用基础Python进行MD文件段落结构转换的方法。通过逐行读取并分析,将原有二级标题提升为三级,三级标题提升为四级,以此类推,实现段落结构的重组。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

服务器上跑程序,除了基础python外,连re library都是不能有的,于是只能搞字符操作完成。记一笔,以备日后还要跑上去。

###为了把某md文件的段落结构重组,比如原来的##变为###, ###变为####。
result_file = open("result_file.md","w")


with open('formalizing_rq.md', 'r') as f:

    for line in f:
        the_list_line=[]
        the_list_line = line.split(None, 1)  # add only first word
        if the_list_line != []:
            the_first_word = the_list_line[0]
            first_word_list = list(the_first_word) # convert string to list
            symbol_of_first_word = list(set(first_word_list))  #simplify the items in the first string
            str_symbol = ''.join(str(e) for e in symbol_of_first_word)
            if str_symbol == '#':
                the_new_first_word = the_first_word + "#"
                the_list_line[0] = the_new_first_word
                result_file.write(' '.join(the_list_line))
                the_list_line="\n"


            else:
                result_file.write(line)
                the_list_line="\n"


        else:

            result_file.write(line)



result_file.close()

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

取啥都被占用

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值