标签:是你 目的 app size lin 结果 sea 自动构建 ng2
背景:
继前博客浅谈代码自动构建工具,目标是用python操作文件,进行读写。
之前没写过python代码,也是第一次撸python,遇到了不少坑,废话不多说,首先放上最好用的干货。
干货:
例如有个文件test.js,现在需要
1 var a = 1 2 var b = 2 3 var c = 3 4 var GameType = "MaJiang20180418" 5 var d = 4 6 var e = 5 7 var GameType = "MaJiang20180418" 8 var Gametype = "DouDiZhu20180418" 9 var f = 8 10 var GameType = "MaJiang20180418" 11 var a = 1 12 var b = 2 13 var c = 3
1 @echo off 2 python test.py 3 pause
1 import os, re 2 list = [] 3 hang = 0 4 findword = r‘^(var GameType = "MaJiang20180418")‘ 5 findInsert = ‘我是你爷爷\n‘ 6 wordInsert = ‘我是你爸爸\n‘ 7 with open("test.js", "r", encoding = "utf-8") as f: 8 lines = f.readlines() 9 with open("test.js", "w", encoding = "utf-8") as f_w: 10 for k, v in enumerate(lines): 11 if(bool(re.search(findword, v))): 12 list.append(k+1) 13 if list: 14 print(list) 15 print("插入行:", list[-1]) 16 lines.insert(list[-1], findInsert) 17 list = [] 18 lines.insert(3, wordInsert) 19 lines.insert(5, wordInsert) 20 f_w.write("".join(lines))
运行完test.bat后的结果:
至此,达到了预期效果
标签:是你 目的 app size lin 结果 sea 自动构建 ng2
原文地址:https://www.cnblogs.com/mikeCao/p/8880812.html