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 ...
随机推荐
- python进制转化函数,10进制字符串互转,16进制字符串互转
来了老弟,emmmmm,今天想到平时经常用到编码转化,把字符串转化为16进制绕过等等的,今天想着用python写个玩,查询了一些资料,看了些bolg 上面的两个函数是将二进制流转化为16进制,data ...
- ubuntu 16.04扩充root 分区
ubuntu使用过程中,提示root分区剩余空间不足,剩余200多M时还可以进行一些操作,剩余几M时拷贝等命令都不能够执行. 扩充root分区步骤如下: 1.查看root分区所在位置: 命令: sud ...
- OpenCV中Mat总结
一.数字图像存储概述 数字图像存储时,我们存储的是图像每个像素点的数值,对应的是一个数字矩阵. 二.Mat的存储 1.OpenCV1基于C接口定义的图像存储格式IplImage*,直接暴露内存,如果忘 ...
- VUE 导出Excel(iframe)
1. 概述 1.1 说明 在开发过程中,有时候需要导出某数据表格(excel)以便客户使用,使用iframe对返回二进制文件进行下载处理.记录此功能,以便后期使用. 2. 示例 2.1 vue示例代码 ...
- Python 通用爬虫 和讯博客 scrapy
目标站点需求分析 通用爬虫,获取和讯博客所有博文 涉及的库 scrapy,re,requests,mysql URL RULE 解析单页源码 保存到数据库 结果
- 七天开发进度(六)(微信小程序版(一))
1. 今天主要根据网上教程学习了微信小程序的代码结构,和代码编写-Tabbar配置, 学习了app.js的App和Page方法, 认识了view组件,button组件,input组件,但是还没怎么应用 ...
- sass进阶—函数
/*内置函数*/ /*1)常规的rgb,rgba函数*/$color:rgb(255,255,162);body{ color: $color; background-color:rgba($colo ...
- Ubuntu下 安装MiniGUI
1. 需要下载的组件 首先需要这些安装包,这些安装包可以在MiniGUI官网上下载. libminigui-gpl-3_0_12.tar.gzmg-samples-3_0_12.tar.gzfreet ...
- 一起学爬虫——使用xpath库爬取猫眼电影国内票房榜
之前分享了一篇使用requests库爬取豆瓣电影250的文章,今天继续分享使用xpath爬取猫眼电影热播口碑榜 XPATH语法 XPATH(XML Path Language)是一门用于从XML文件中 ...
- 安装酷痞到IIS7.x共用80端口Windows(64位)系统下运行多个酷痞
需求: 1.酷痞直接运行的模式是自宿主运行.由于win系统一般都由iis提供多个网站服务,并首先占用了80端口,如果想酷痞可以直接通过主机头和iis共用80端口会出现这种运行模式的需求. 以下是实现方 ...