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 ...
随机推荐
- C语言官网蓝桥杯训练1115DNA
这道题是比较简单的输出字符图形的题,但是有几个坑还是要注意下. 1.题中所述的X是大写的,不要看成小写了.(我就错了好几次) 2.每一行输出最后的X后不能在输出空格. 3.输出两个DNA中间有一个空行 ...
- jquery前端倒计时
function FreshTime(){ // var endtime=new Date("2019/04/15,12:20:12");//结束时间 var endtime = ...
- docker镜像的使用及相关
参考网站docker中文网:http://www.docker.org.cn/book/docker/docker-push-image-13.html 1.搜索容器: docker search t ...
- 七.HTTP协议原理介绍
01. 当用户访问一个网站时,都发生了事情? ①. 利用DNS服务,将输入的域名解析为相应的IP地址 a --本地主机输入域名后,会查询本地缓存信息和本地hosts文件 如果有就进行解析,如果没有 ...
- Discovery Scanning
1.NetDiscover you performe layer 2 the comand : netdiscover -r 192.168.2.0/24 or use netdiscove ...
- SpringMvc+Hibernate+Mysql保存表情字符(昵称)到数据库报错的问题?
背景: 一个中小型H5游戏 描述: 游戏通过微信授权登入, 获取到用户昵称并将用户信息保存至Mysql数据库, 当遇到有些用户微信昵称中带有表情(特殊字符)时, 保存至数据库出错! 核心错误: Cau ...
- 微信开发getLocation、openLocation等一些功能不起作用,但是走ready方法 closeWindow一些方法可以用
1.检查wx.config,发现我在jsApiList也声明了这些方法,并且也走了ready回调 wx.config({ debug: false, // 开启调试模式,调用的所有api的返回值会在客 ...
- 使用 Sonar 检测代码质量
经历了一段时间的加班赶项目进度之后,今天终于闲下来了.忽然不知道干啥.于是,想着做点什么吧.突然想起了码云上面有个代码分析的功能,用的是 Sonar 于是想来玩玩这个. 一.下载Sonar,和初始化, ...
- python面试题之什么是PEP8规范
1 缩进.4个空格的缩进(编辑器都可以完成此功能),不使用Tap,更不能混合使用Tap和空格. 2 每行最大长度79,换行可以使用反斜杠,最好使用圆括号.换行点要在操作符的后边敲回车. 3 类和top ...
- Java 常用知识点
Java 常用知识点 1.日期格式化 SimpleDateFormat Date date=new Date(System.currentTimeMillis()) ; SimpleDateForma ...