运行环境:python3.9
很多时候调整参考文献引用顺序时,需要重新编排前后序号,该脚本实现txt中调整前后顺序,编号自动重生成重排
运行前:
运行后在当前文件夹生成“引用new.txt":
源代码:
# -*- coding:utf-8 -*-
# ---------------------------------------------------------------------------
# Author: LGZ
# Created on:
# Reference:
# coding:cp936 or coding:utf-8
# ---------------------------------------------------------------------------
import logging,os,csv
if __name__ == '__main__':
"""脚本单独使用时运行以下内容"""
"""----------------------------------------------"""
"""---------------------PARA---------------------"""
txtFileName = ".\引用.txt"
outFileName = "引用new.txt"
with open(txtFileName, 'r', encoding="utf-8") as file:
with open(outFileName, 'w', encoding="utf-8") as new_file:
n = 1
lst = []
newline = [line for line in file if line.strip() != ""]
for i0 in newline:
if "[" in i0:
lst.append(i0)
elif "[" not in i0:
lst[-1] = lst[-1].rstrip()+" " + i0
for i1 in lst:
lst1 = list(i1)
left, right = lst1.index("["),lst1.index("]")
lst2= lst1[:left+1] +[n]+ lst1[right:]
new_file.write("".join([str(i) for i in lst2]))
n += 1
(如有指正或需要定制功能欢迎联系)