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中最常用的数据类型.我们可以使用引号('或")来创建字符串. 创建字符 ...
随机推荐
- jquery实现上下浮动
jquery实现上下浮动: <!DOCTYPE html> <html> <head> <meta charset="utf-8"> ...
- json数据字典,以及数据在下拉框中显示
建立person_vocation.json数据字典文件,内容: [ {"id":1,"disabled":false,"selected" ...
- c# 结构 struct
结构是使用 struct 关键字定义的,与类相似,都表示可以包含数据成员和函数成员的数据结构. 一般情况下,我们很少使用结构,而且很多人也并不建议使用结构,但作为.NET Framework 一般型別 ...
- Ubuntu18.04 解压zip文件乱码的解决方法
在Ubuntu的系统下解压zip文件的时候居然出现了乱码,通过查找网上的资料,解决的办法有两种 一.通过unzip行命令解压,指定字符集,由于zip格式中并没有指定编码格式,Windows下生成的zi ...
- JDBC连接ORACLE无法登陆java.sql.SQLException: ORA-01017: invalid username/password; logon denied
当用jdbc连接Oracle数据库的时候 private Connection getConnection() throws SQLException { OracleDataSource ods = ...
- caioj 1077 动态规划入门(非常规DP1:筷子)
首先可以看出排序之后,最优解肯定是每一对都相邻才是最优的 那么我们就要找构成最优解的相邻组 设f[i][j]是前i个字符,k对的最小值 如果当前这个筷子不取的话,f[i][j] = f[i-1][j] ...
- Unity经验之谈
1.全屏与非全屏之间的切换 if (Input.GetMouseButtonDown(1)) { Screen.fullScreen = !Screen.fullScreen; } 2.Camera适 ...
- [Python] Use a Python Generator to Crawl the Star Wars API
In this lesson, you will be introduced to Python generators. You will see how a generator can replac ...
- 基于请求响应的MVC框架调用分析
一.使用Servlet来处理请求响应 当client提交数据之后.接着发送请求,请求被封装成对象,server接收到请求,依据请求的URL.来推断将请求对象交由哪个Servlet处理. 在servle ...
- Lesson 1 Basic Concepts: Part 1
www.how-to-build-websites.com/basic-concepts/part1.php An introduction to domain names, web servers, ...