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 ...
随机推荐
- WPF 之 调用线程必须为 STA,因为许多 UI 组件都需要
WPF中,代码中准备控制控件内容时,有时会报错:“ 调用线程必须为 STA,因为许多 UI 组件都需要 ”. 如在winform下面,使用多线程时,控件的值读取是可以的,但如果要更改,那么就必须进行一 ...
- Go学习之路
注:图片来自 https://github.com/gocn/knowledge, 更新在此,共勉前行者.
- linux安装selenium+chrome+phantomjs
1. 安装 selenium pip3 install selenium pip3 安装参考 2. 安装 ChromeDriver yum install chromedriver.x86_64 3. ...
- Vue项目中使用webpack配置了别名,引入的时候报错
chainWebpack(config) { config.resolve.alias .set('@', resolve('src')) .set('assets', resolve('src/as ...
- liunx 下WebBench 安装与压力测试
安装: wget http://blog.zyan.cc/soft/linux/webbench/webbench-1.5.tar.gz tar zxvf webbench-1.5.tar.gz cd ...
- 项目启动,main函数之前的代码执行两次 restartedMain
https://blog.csdn.net/qq_35981283/article/details/78925146
- hibernate 保存报错 Hibernate operation: could not get next sequence value;
错误信息: [2017-09-28 18:51:38,854]-[org.hibernate.util.JDBCExceptionReporter:101] ERROR - Numeric Overf ...
- 迭代DOM集合的几种方法
1. Array.prototype.slice.call() 转数组再遍历 var a= document.querySelectorAll('div'); var arr = Array. ...
- call、apply、bind
***call,apply,bind 替换this 何时: 只要this不是想要的都可用call,apply,bind替换 选择: call/apply: *调用*函数,在调用时,*临时*替换函数中的 ...
- 斯特林公式 ——Stirling公式(取N阶乘近似值)