#!/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遍历文件(替换)的更多相关文章

  1. python 遍历文件夹 文件

    python 遍历文件夹 文件   import os import os.path rootdir = "d:\data" # 指明被遍历的文件夹 for parent,dirn ...

  2. python遍历文件夹下的文件

    在读文件的时候往往需要遍历文件夹,python的os.path包含了很多文件.文件夹操作的方法.下面列出: os.path.abspath(path) #返回绝对路径 os.path.basename ...

  3. Python遍历文件个文件夹

    在读文件的时候往往需要遍历文件夹,python的os.path包含了很多文件.文件夹操作的方法.下面列出: os.path.abspath(path) #返回绝对路径 os.path.basename ...

  4. 使用python遍历文件夹取出特定的字符串

    # -*- coding: utf-8 -* import re import os # 需要处理的文件夹路径(绝对路径) path = u"/Users/a140/Downloads/te ...

  5. Python 遍历文件夹清理磁盘案例

    import os suffix_name_list = [".pdb", ".ilk"] def find_file(path): # 遍历文件夹 for i ...

  6. python 遍历文件夹下的所有文件

    基础 import os # 遍历文件夹 def walkFile(file): for root, dirs, files in os.walk(file): # root 表示当前正在访问的文件夹 ...

  7. Python遍历文件夹

    许多次需要用python来遍历目录下文件, 这一次就整理了记录在这里. 随实际工作,不定期更新. import os class FileTraversal: def __init__(self, r ...

  8. Python遍历文件夹和读写文件的方法

    需 求 分 析 1.读取指定目录下的所有文件2.读取指定文件,输出文件内容3.创建一个文件并保存到指定目录 实 现 过 程 Python写代码简洁高效,实现以上功能仅用了40行左右的代码~ 昨天用Ja ...

  9. python遍历文件夹中所有文件夹和文件,os.walk

    python中可以用os.walk来遍历某个文件夹中所有文件夹和文件. 例1: import os filePath = 'C:/Users/admin/Desktop/img' for dirpat ...

随机推荐

  1. from中buttone 和 input type="button" 区别

    在做一个表单提交时碰到的问题, 1.js判断阻止表单提交,如果是form 里面的button的话,恭喜你,你要再换个写法了.<button type="submit" ... ...

  2. 关于 min_25 筛的入门以及复杂度证明

    min_25 筛是由 min_25 大佬使用后普遍推广的一种新型算法,这个算法能在 \(O({n^{3\over 4}\over log~ n})\) 的复杂度内解决所有的积性函数前缀和求解问题(个人 ...

  3. vue 报错总结

    关闭vue-cli 默认eslint规则: 找到 build -> webpack.base.config.js ,删除箭头指向代码 1.Newline required at end of f ...

  4. 列表生成式,迭代器&生成器

    python3中range(10)就 是迭代器 列表生成式 #列表生成式 a=[0,1,2,3,4,5] b=[] for index,i in enumerate(a): a[index]+1 pr ...

  5. SpringBoot配置

    多模块Maven项目 .gitignore文件 .idea *.iml targetout log tmp test 父模块pom文件 <?xml version="1.0" ...

  6. IDEA打包jar包

    1.点击idea左边的Maven 2.点开要打包的项目下的-->Lifecycle-->双击package 3.jar包会默认保存在项目下的target文件夹下

  7. 使用Selenium+ChromeDriver登录微博并且获取cookie

    using OpenQA.Selenium;using OpenQA.Selenium.Chrome; public class GetSinaCookie { private static stri ...

  8. python第九天(9-33)

    一:进程 进程概念 进程就是一个程序运行在一个数据集上的一次动态执行过程 进程一般由程序,数据集,进程控制块组成 进程控制块: 进程控制块用来记录进程的外部特征,描述进程的执行变化过程,系统可以利用它 ...

  9. ThinkPHP5配置redis缓存

    thinkphp采用cache类提供缓存功能支持,采用驱动方式,在使用缓存之前需要进行初始化操作.支持的缓存类型包括file.memcache.wincache.sqlite.redis和xcache ...

  10. MyString

    [摘自C++程序设计语言] MyString.h #include <cstring> #include <iostream> #include <stdexcept&g ...