python-实现xml字符串替换功能
今天遇到一个问题,说的是要把一个android res目录下,所有name=xx的字符串的值,自己参照网上的方法,写了一个脚本。记录如下,方便以后使用
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import os
import re #定义一个函数,筛选所有文件
#list files
def listFiles(dirPath):
#walk方法,root-文件夹路径;dirs-含有的子文件夹;files-含有的文件
for root,dirs,files in os.walk(dirPath):
for fileObj in files:
if "-es" in root:
#获得某个文件的绝对路径并添加到列表中
fileList_es.append(os.path.join(root,fileObj))
elif "-en" in root:
fileList_en.append(os.path.join(root,fileObj))
elif "-fr" in root:
fileList_fr.append(os.path.join(root,fileObj))
else:
fileList_nor.append(os.path.join(root,fileObj))
#定义一个函数,更改字符串
def changeString(style):
changeText = ""
if style == "en":
fileList = fileList_en
changeText = '"You are going to use the roaming data service ,any price information please contact your service provider"'
elif style == "es":
fileList = fileList_es
changeText = '"Usted va a utilizar el servicio de datos en itinerancia, cualquier información de precios por favor póngase en contacto con su proveedor de servicios"'
elif style == "fr":
fileList = fileList_fr
changeText = '"Vous allez utiliser le service de données en itinérance, les informations de prix s\'il vous plaît contacter votre fournisseur de services"'
else:
fileList = fileList_nor
changeText = '"You are going to use the roaming data service ,any price information please contact your service provider"' for fileObj in fileList:
#以读写模式打开文件
f = open(fileObj,'r+')
#读取所有内容,每一行放到一个列表里面
all_the_lines=f.readlines()
#光标移动到文章开头
f.seek(0)
#这个是截取文章,它是把文章清空了,感觉不合理,明显影响了效率
f.truncate() for line in all_the_lines:
print(line)
#print (pattern)
#用来获得匹配的字符
res = pattern.search(line)
if res != None:
res = res.groups()
#把每一行的内容替换掉了以后重新写入
f.write(line.replace(res[0],changeText))
else:
f.write(line) f.close()
def main():
#考虑到脚本的通用性,这个尽量不要写死
#fileDir = "/mnt/zwx318792/hq6735/packages/services/Telephony/res"
fileDir = '.' listFiles(fileDir)
changeString("en")
changeString("es")
changeString("fr")
changeString("nor")
#这个是用来生成一个正则表达式条件
pattern = re.compile('<string name="roaming_warning".*?>(.*)</string>')
fileList_en=[]
fileList_es=[]
fileList_fr=[]
fileList_nor=[]
#这个好像是python脚本的固定写法
if __name__=='__main__': main()
exit()
python-实现xml字符串替换功能的更多相关文章
- python 字符串替换功能 string.replace()可以用正则表达式,更优雅
说起来不怕人笑话,我今天才发现,python 中的字符串替换操作,也就是 string.replace() 是可以用正则表达式的. 之前,我的代码写法如下,粗笨: 自从发现了正则表达式也生效后,代码变 ...
- Python中str字符串的功能介绍
Str字符串的功能介绍 1. 字符串的操作 字符串的连接操作 符号: + 格式:str1 + str2 例如:str1 = 'I Love' str2 = 'You!' print(str1 + st ...
- python文件操作--字符串替换
如把test.txt文件的 所有 AAA 字符串 替换成 aaaaa with open('test.txt','+r') as f: t = f.read() t = d.replace('AAA' ...
- python基础之字符串基本功能
终于还是踏上了Python的不归路,不知道能不能走的完. 先总结一下今天学习的字符串的各个功能吧:只写了部分用的比较多的. 1.capitalize: 字符串首字母大写 >>> na ...
- python translate maketrans 字符串替换
string1='abcd-1234' print(string1.translate(string1.maketrans('abc','ABC'))) a='aeiou' b=' string2=' ...
- Python正则表达式如何进行字符串替换实例
Python正则表达式如何进行字符串替换实例 Python正则表达式在使用中会经常应用到字符串替换的代码.有很多人都不知道如何解决这个问题,下面的代码就告诉你其实这个问题无比的简单,希望你有所收获. ...
- oracle学习笔记:字符串替换 replace、regexp_replace、translate函数
1.replace 函数 语法:replace(char, search_string, replacement_string) --针对字符串替换 功能: 将char中的字符串替换. 当re ...
- 任务备忘(已经完成):用python写一个格式化xml字符串的程序
功能: 1.将xml中多余的空格,换行符去掉,让xml字符串变成一行. 2.将xml中添加缩进,使用print能正确打印添加缩进后的字符串. 思路: 采用正则表达式来判断xml中字符串的类型: 1.文 ...
- Python 字符串_python 字符串截取_python 字符串替换_python 字符串连接
Python 字符串_python 字符串截取_python 字符串替换_python 字符串连接 字符串是Python中最常用的数据类型.我们可以使用引号('或")来创建字符串. 创建字符 ...
随机推荐
- [HNOI2008] GT考试(DP+矩阵快速幂+KMP)
题目链接:https://www.luogu.org/problemnew/show/P3193#sub 题目描述 阿申准备报名参加 GT 考试,准考证号为 N 位数 X1,X2…Xn(0 <= ...
- Hadoop框架基础(三)
** Hadoop框架基础(三) 上一节我们使用eclipse运行展示了hdfs系统中的某个文件数据,这一节我们简析一下离线计算框架MapReduce,以及通过eclipse来编写关于MapReduc ...
- vue中计算小数保留两位小数
代码
- 禁用cache
Z:\src\services\network\network_context.cc:http_cache_enabled
- Chrome Foundation Services
Chrome Foundation Services Contents Overview API Standards Service Directory Structure Dependencies ...
- Division Game UVA - 11859 Nim
Code: #include<cstdio> #include<algorithm> using namespace std; #define maxn 10005 int n ...
- Linux 部署项目经验总结
[通用命令] 1.创建文件夹 mkdir -p xxx 2.解压包 tar -zxvf xxxx.tar.gz 3.缩文件 tar zcvf 压缩包名称.tar.gz 要压缩的文件 4.动命令 ...
- Java基础学习总结(11)——重载与重写
首先我们来讲讲:重载(Overloading) 一.方法的重载 方法名一样,但参数不一样,这就是重载(overload). 所谓的参数不一样,主要有两点:第一是参数的个数不一样,第二是参数的类型不一样 ...
- volatile的含义
从词面上来讲.volatile的意思是易变的,也就是说.在程序执行的过程中,有一些变量可能会被莫名其妙的改变,而优化器为了节约时间.有时候不会重读这个变量的真实值,而是去读在寄存器的备份,这种话,这个 ...
- c3p0出现 An attempt by a client to checkout a Connection has timed out
java.sql.SQLException: An attempt by a client to checkout a Connection has timed out. at com.mchange ...