今天遇到一个问题,说的是要把一个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字符串替换功能的更多相关文章

  1. python 字符串替换功能 string.replace()可以用正则表达式,更优雅

    说起来不怕人笑话,我今天才发现,python 中的字符串替换操作,也就是 string.replace() 是可以用正则表达式的. 之前,我的代码写法如下,粗笨: 自从发现了正则表达式也生效后,代码变 ...

  2. Python中str字符串的功能介绍

    Str字符串的功能介绍 1. 字符串的操作 字符串的连接操作 符号: + 格式:str1 + str2 例如:str1 = 'I Love' str2 = 'You!' print(str1 + st ...

  3. python文件操作--字符串替换

    如把test.txt文件的 所有 AAA 字符串 替换成 aaaaa with open('test.txt','+r') as f: t = f.read() t = d.replace('AAA' ...

  4. python基础之字符串基本功能

    终于还是踏上了Python的不归路,不知道能不能走的完. 先总结一下今天学习的字符串的各个功能吧:只写了部分用的比较多的. 1.capitalize: 字符串首字母大写 >>> na ...

  5. python translate maketrans 字符串替换

    string1='abcd-1234' print(string1.translate(string1.maketrans('abc','ABC'))) a='aeiou' b=' string2=' ...

  6. Python正则表达式如何进行字符串替换实例

    Python正则表达式如何进行字符串替换实例 Python正则表达式在使用中会经常应用到字符串替换的代码.有很多人都不知道如何解决这个问题,下面的代码就告诉你其实这个问题无比的简单,希望你有所收获. ...

  7. oracle学习笔记:字符串替换 replace、regexp_replace、translate函数

    1.replace 函数 语法:replace(char, search_string, replacement_string) --针对字符串替换 功能: ​ 将char中的字符串替换. ​ 当re ...

  8. 任务备忘(已经完成):用python写一个格式化xml字符串的程序

    功能: 1.将xml中多余的空格,换行符去掉,让xml字符串变成一行. 2.将xml中添加缩进,使用print能正确打印添加缩进后的字符串. 思路: 采用正则表达式来判断xml中字符串的类型: 1.文 ...

  9. Python 字符串_python 字符串截取_python 字符串替换_python 字符串连接

    Python 字符串_python 字符串截取_python 字符串替换_python 字符串连接 字符串是Python中最常用的数据类型.我们可以使用引号('或")来创建字符串. 创建字符 ...

随机推荐

  1. BZOJ 2424 DP OR 费用流

    思路: 1.DP f[i][j]表示第i个月的月底 还剩j的容量 转移还是相对比较好想的-- f[i][j+1]=min(f[i][j+1],f[i][j]+d[i]); if(j>=u[i+1 ...

  2. iOS菜鸟成长笔记(2)——网易彩票练习

    距离上一篇<第一个iOS应用>已经有一个多月了,今天来和大家一起学习和分享一下一个小练习<网易彩票> 首先我们向storyboard中拖入一个TabBarController和 ...

  3. Android属性动画-Interpolator和ViewPropertyAnimator的用法

    Interpolator的用法 Interpolator这个东西很难进行翻译,直译过来的话是补间器的意思,它的主要作用是可以控制动画的变化速率,比如去实现一种非线性运动的动画效果.那么什么叫做非线性运 ...

  4. <Sicily> Longest Common Subsequence

    一.题目描述 Given a sequence A = < a1, a2, -, am >, let sequence B = < b1, b2, -, bk > be a s ...

  5. Pepper plugin implementation

    For Developers‎ > ‎Design Documents‎ > ‎ Pepper plugin implementation This document provides a ...

  6. 并查集 (Union Find ) P - The Suspects

    Severe acute respiratory syndrome (SARS), an atypical pneumonia of unknown aetiology, was recognized ...

  7. POJ-2142 The Balance 扩展欧几里德(+绝对值和最小化)

    题目链接:https://cn.vjudge.net/problem/POJ-2142 题意 自己看题吧,懒得解释 思路 第一部分就是扩展欧几里德 接下来是根据 $ x=x_0+kb', y=y_0- ...

  8. clear---清除当前屏幕

    clear命令用于清除当前屏幕终端上的任何信息.

  9. 监控web服务(http,本地 / 远程监控nginx)

    监控 httpd 服务一: #!/bin/bash #描述: 秒级别监控 http 服务 while [ 1 -lt 2 ] do sleep 10 ai=`netstat -ntl | grep & ...

  10. Vue2.0组件实现动态搜索引擎(一)

    原文链接:https://blog.csdn.net/qwezxc24680/article/details/74550556 从github上看到一个不错的开源项目:https://github.c ...