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'))
原文地址:https://www.cnblogs.com/jian-dan-ai-boke/p/10268920.html