python遍历文件(替换)
#!/usr/local/bin/python
# -*- coding: UTF-8 -*-
#coding:gbk
import re
import os
w_str=""
x="assets2.res.json"
fopen=open(x,'r')
number=1
for line in fopen:
list1=re.findall(r"\"name\"\:\"(.+?)\"",line)
list2=re.findall(r"\"keys\"\:\"(.+?)\"",line)
list3=re.findall(r"\"subkeys\"\:\"(.+?)\"",line)
if(number>83):
if list1:
for xxx in list1:
line=re.sub(xxx,'dui'+xxx,line)
w_str+=line
elif list3:
for xxx in list3:
if "," in xxx:
listparams=xxx.split(",")
listparams.sort(key=lambda i:len(i),reverse=True)
for xxxx in listparams:
line=re.sub(xxxx,'dui'+xxxx,line)
for ttt in listparams:
for aaa in listparams:
if ttt!=aaa:
if aaa.find(ttt)>0:
print(ttt)
bbb=aaa.replace(ttt,"")
line=re.sub('dui'+bbb+'dui'+ttt,'dui'+bbb+ttt,line)
w_str+=line
else:
line=re.sub(xxx,'dui'+xxx,line)
w_str+=line
else:
w_str+=line
else:
if list1:
for xxx in list1:
line=re.sub(xxx,'dui'+xxx,line)
w_str+=line
elif list2:
#print (list2)
if "," in list2:
print(number)+str(number)
listparams=list2.split(",")
for xxx in listparams:
print "number"+number
line=re.sub(xxx,'dui'+xxx,line)
w_str+=line
else:
for xxx in list2:
if "," in xxx:
listparams=xxx.split(",")
listparams.sort(key=lambda i:len(i),reverse=True)
for xxxx in listparams:
line=re.sub(xxxx,'dui'+xxxx,line)
for ttt in listparams:
for aaa in listparams:
if ttt!=aaa:
if aaa.find(ttt)>0:
print(ttt)
bbb=aaa.replace(ttt,"")
line=re.sub('dui'+bbb+'dui'+ttt,'dui'+bbb+ttt,line)
w_str+=line
else:
line=re.sub(xxx,'dui'+xxx,line)
w_str+=line
else:
w_str+=line
number=number+1
wopen=open(x,'w')
wopen.write(w_str)
fopen.close()
wopen.close()
raw_input(unicode('enter...','utf-8').encode('gbk'))
#!/usr/local/bin/python
# -*- coding: UTF-8 -*-
#coding:gbk
import re
import os
rootdir = 'assets2' def bianli(str):
list = os.listdir(str) #列出文件夹下所有的目录与文件
for i in range(0,len(list)):
path = os.path.join(str,list[i])
#print path
if os.path.isfile(path):
if ".json" in path:
print path
w_str=""
fopen=open(path,'r')
number=1
for line in fopen:
list1=re.findall(r"\"(.+?)png\"",line)
#print ("list1"+list1)
list2=re.findall(r"\"(.+?)jpg\"",line)
list3=re.findall(r"\"mc\"",line)
if list1:
for xxx in list1:
if ":" in xxx:
w_str+=line
else:
line=re.sub(xxx,'dui'+xxx,line)
w_str+=line
elif list2:
for xxx in list2:
if ":" in xxx:
w_str+=line
else:
line=re.sub(xxx,'dui'+xxx,line)
w_str+=line
elif list3:
if number==1:
break
elif number==2:
break
else:
print("error")
else:
w_str+=line
number=number+1
fopen.close()
if len(w_str)>3:
wopen=open(path,'w')
wopen.write(w_str)
wopen.close()
else:
bianli(path)
bianli(rootdir)
raw_input(unicode('enter...','utf-8').encode('gbk'))
python遍历文件(替换)的更多相关文章
- python 遍历文件夹 文件
python 遍历文件夹 文件 import os import os.path rootdir = "d:\data" # 指明被遍历的文件夹 for parent,dirn ...
- python遍历文件夹下的文件
在读文件的时候往往需要遍历文件夹,python的os.path包含了很多文件.文件夹操作的方法.下面列出: os.path.abspath(path) #返回绝对路径 os.path.basename ...
- Python遍历文件个文件夹
在读文件的时候往往需要遍历文件夹,python的os.path包含了很多文件.文件夹操作的方法.下面列出: os.path.abspath(path) #返回绝对路径 os.path.basename ...
- 使用python遍历文件夹取出特定的字符串
# -*- coding: utf-8 -* import re import os # 需要处理的文件夹路径(绝对路径) path = u"/Users/a140/Downloads/te ...
- Python 遍历文件夹清理磁盘案例
import os suffix_name_list = [".pdb", ".ilk"] def find_file(path): # 遍历文件夹 for i ...
- python 遍历文件夹下的所有文件
基础 import os # 遍历文件夹 def walkFile(file): for root, dirs, files in os.walk(file): # root 表示当前正在访问的文件夹 ...
- Python遍历文件夹
许多次需要用python来遍历目录下文件, 这一次就整理了记录在这里. 随实际工作,不定期更新. import os class FileTraversal: def __init__(self, r ...
- Python遍历文件夹和读写文件的方法
需 求 分 析 1.读取指定目录下的所有文件2.读取指定文件,输出文件内容3.创建一个文件并保存到指定目录 实 现 过 程 Python写代码简洁高效,实现以上功能仅用了40行左右的代码~ 昨天用Ja ...
- python遍历文件夹中所有文件夹和文件,os.walk
python中可以用os.walk来遍历某个文件夹中所有文件夹和文件. 例1: import os filePath = 'C:/Users/admin/Desktop/img' for dirpat ...
随机推荐
- Gulp实战
推荐文章: gulp.js中文网 : http://www.gulpjs.com.cn/ DBPOO : http://www.dbpoo.com/getting- ...
- ROS-十步完成ROS-indigo安装
Ubuntu 版本是14.04.5.(这个版本的ubuntu的内核是V4.4的内核,长期维护到2019年,是14代比较稳定的,反正用这个安装就没有什么依赖的头疼的问题,安装ROS很方便,可以去清华的网 ...
- eclipse创建maven——初学
1.进入eclipse→File→new→other→搜索maven,如下图: 2.选择一个工作空间,点击Next 3.进入如下页面 4.填写Grop id和Artifact id,Version默认 ...
- MySQL查询表中某个字段的重复数据
1. 查询SQL表中某个字段的重复数据 SELECT user_name,COUNT(*) AS count FROM db_user_info GROUP BY user_name HAVING c ...
- git生成ssh key步骤并添加到github网站
0: 查看是否已经有了ssh密钥 执行命令:cd ~/.ssh 如果没有密钥则不会有此文件夹,有则备份删除 1:使用 Git Bash生成新的ssh key ssh-keygen -t rsa -C ...
- sklearn模型的属性与功能-【老鱼学sklearn】
本节主要讲述模型中的各种属性及其含义. 例如上个博文中,我们有用线性回归模型来拟合房价. # 创建线性回归模型 model = LinearRegression() # 训练模型 model.fit( ...
- 5.基于优化的攻击——CW
CW攻击原论文地址——https://arxiv.org/pdf/1608.04644.pdf 1.CW攻击的原理 CW攻击是一种基于优化的攻击,攻击的名称是两个作者的首字母.首先还是贴出攻击算法的公 ...
- 理解ActivityManagerService
--摘自<Android进阶解密> *AMS家族* 1.Android 7.0的AMS家族 2.AMP和AMS进行通信 3.(Android 7.0)AMP是AMN的内部类,它们都实现了I ...
- Springboot中使用Scheduled做定时任务
在开发中,定时任务是常见的功能,在spring boot 下开发定时任务其实很简单,具体代码如下: 1.配置依赖包pom.xml 由于默认的maven仓库经常访问不了,这里采用了阿里云的maven仓库 ...
- JZOJ5431 捕老鼠
JZOJ 5341 Description 为了加快社会主义现代化,建设新农村,农夫约(Farmer Jo)决定给农庄里的仓库灭灭鼠.于是,猫被农夫约派去捕老鼠. 猫虽然擅长捕老鼠,但是老鼠们太健美了 ...