今天遇到一个问题,说的是要把一个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. jzoj3454 表白(love)解题报告(01分数规划+DP)

    题目链接:https://jzoj.net/senior/#contest/show/2414/2 题目描述: 鸡腿是CZYZ的著名DS,但是不想追妹子的DS不是好GFS,所以鸡腿想通过表白来达到他追 ...

  2. JS实现文字图片无缝滚动

    今天做项目遇到一个滚动的效果,本来打算用marquee做的,因为它是html自带的标签,写起来简单,但是有一个问题就是marquee不能实现无缝滚动,上网找了一些方法,发现marquee可以实现无缝, ...

  3. jquery on event

    <!doctype html> <html lang="en"> <head> <meta charset="utf-8&quo ...

  4. MFC 创建新项目

    本文主要介绍如何是使用vc++创建MFC项目,并简单介绍整个项目的基本框架.文件–>新建,会弹出以下新建文件(项目)类型的界面,如下 按上图的步骤u,创建一个MFC工程.其中步骤3中的CardD ...

  5. Java中将String转json对象

    import org.json.simple.JSONObject; import org.json.simple.parser.JSONParser; import org.json.simple. ...

  6. core组件进阶

    访问图像像素 存储方式 BGR连续存储有助于提升图像扫描速度. isContinuous()判断是否是连续存储. 颜色空间缩减 仅用这些颜色中具有代表性的很小的部分,就足以达到同样的效果. 将现有颜色 ...

  7. iOS——集成支付宝 系统繁忙,请稍后再试ALI10

    问题描述:调用支付宝时,显示系统繁忙,请稍后再试(ALI10).代码没有报错,其他也是按照文档来的,为何老是提示显示系统繁忙? 解决方案:还需要在targets的中info里面,添加  url typ ...

  8. 钩子(hooks)—webhook-使用钩子自动触发部署

    钩子(hooks)-webhook http://fighter.blog.51cto.com/1318618/1670667 https://www.lovelucy.info/auto-deplo ...

  9. 參加北京bluemix云计算大会偶记

    我就不写散文了.博客也要轻量化. 记录心路历程吧. 这是一次ibm的技术大会.也是传道大会,洗脑大会.会议主题看起来非常多,占领了北京国际饭店的三层,作为一个老ibm bp感受非常多. 1.北京的创业 ...

  10. POJ 1887 Testingthe CATCHER (LIS:最长下降子序列)

    POJ 1887Testingthe CATCHER (LIS:最长下降子序列) http://poj.org/problem?id=3903 题意: 给你一个长度为n (n<=200000) ...