用python将%格式化字符串转换成xliff格式
2017-06-21 21:11
316 查看
1. xliff是什么
xliff是Android中的用以格式化字符串的一种东西。%d这种会转换成<xliff:g id="xxx">%1$d</xliff:g>这种。具体见https://developer.android.com/guide/topics/resources/string-resource.html
2. 代码
class XliffMaker(object): """class for XliffMaker""" def __init__(self, s): super(XliffMaker, self).__init__() self.s = s def make(self): reg = re.compile(STRING_REGEX) idx = 1 targetStrHead = '<xliff:g id="FMT_STR_ID">' targetStrBoot = '</xliff:g>' targetStr = targetStrHead mediemStr = "" otherStrList = reg.split(self.s) replacedList = [] for m in reg.finditer(self.s): s0 = m.group() s1 = "" if 'd' in s0: reg0 = re.compile(r'%(\d+)d') s1 = '%%%d$%sd' % (idx, reg0.search('%2d').group(1)) elif 's' in s0: s1 = '%%%d$s' % idx replacedList.append(s1) idx = idx + 1 ret = zip(otherStrList, replacedList) for t in ret: mediemStr = mediemStr + "".join(t) mediemStr = mediemStr + otherStrList[-1] return targetStr + mediemStr + targetStrBoot
目前只支持%d和%s两种格式!其中,%d可以使用位数限制格式,例如%2d代表2位数。
相关文章推荐
- python从一种时间字符串格式转换到另一种时间字符串格式
- java/python中获取当前系统时间,并与字符串相互转换格式,或者转化成秒数,天数等整数
- 格式化的JSON格式的字符串转换成字典
- Python之time模块的时间戳、时间字符串格式化与转换(13位时间戳)
- python时间戳于时间格式字符串相互转换的方法
- Python中unicode编码的字符串和其他格式的字符串之间进行转换
- python知识:json格式文本;异常处理;字符串处理;unicode类型和str类型转换
- Python将日期时间按照格式转换成字符串
- 神一般的 str.format() 函数 - Python 字符串格式化转换(转载)
- Python 2.7 日期格式化 字符串日期转换
- python 字符串格式化转换类型
- python将字符串格式日期转换为excel的日期格式
- python json格式字符串转换为字典格式
- python3----字符串格式化(format)
- 浅析Js(Jquery)中,字符串与JSON格式互相转换的示例(直接运行实例)
- 将字符串转换成指定编码格式(转载自网络,地址不详,请文章的作者见谅)
- SQL Server中也用格式字符串定制日期转换为字符串
- SQL Server日期时间格式转换字符串详解
- python_12(查找字符串、字符串查找、时间转换、正则、限定符、分组)
- python(map,reduce,filter)以及小例子(计算1-10的和,将大写字母字符串转换成小写字符串)