多国语言strings.xml与excel表格互相转换
2015-05-09 08:16
423 查看
翻译只熟悉excel表格,因此做了互转的工具
#-*- coding=utf-8 -*- import xml.etree.ElementTree import sys def fetchMap(path): strmap={} for line in file(path): line=line.strip() if '<string' in line: pos0=line.find('name="') pos1=line.find('"', pos0+6) pos2=line.find('>', pos1) pos3=line.find('</string>', pos2) val=line[pos2+1:pos3].decode('utf8').encode("gb2312") if "," in val: val='"%s"'%val strmap[line[pos0+6:pos1]] = val # strmap={} # e = xml.etree.ElementTree.parse(path).getroot() # for item in e.findall('string'): # if item.text!=None: # strmap[item.attrib["name"]] = (item.text).encode("gb2312") # else: # strmap[item.attrib["name"]] = "" return strmap strmap=fetchMap('values/strings.xml') strmap2=fetchMap('values-zh-rCN/strings.xml') f_handler=open('out.csv', 'w') sys.stdout=f_handler for k,v in strmap.items(): if k in strmap2: print "%s,%s,%s,"%(k,v,strmap2[k]) else: print "%s,%s,,"%(k,v)
#-*- coding=utf-8 -*- import sys import csv import os def fetchMap(path): strmap={} for line in file(path): line=line.strip() if '<string' in line: pos0=line.find('name="') pos1=line.find('"', pos0+6) pos2=line.find('>', pos1) pos3=line.find('</string>', pos2) val=line[pos2+1:pos3].decode('utf8').encode("gb2312") if "," in val: val='"%s"'%val strmap[line[pos0+6:pos1]] = val return strmap strmap=fetchMap('values/strings.xml') csvmap={} with open('strs.csv') as csvfile: reader = csv.DictReader(csvfile) for row in reader: csvmap[row['id']]=row #sys.stdout=f_handler f0=file("values/strings1.xml", 'w') if not os.path.exists('values-zh-rCN'): os.mkdir('values-zh-rCN') f1=file("values-zh-rCN/strings.xml", 'w') if not os.path.exists('values-pt-rPT'): os.mkdir('values-pt-rPT') f2=file("values-pt-rPT/strings.xml", 'w') if not os.path.exists('values-zh-rTW'): os.mkdir('values-zh-rTW') f3=file("values-zh-rTW/strings.xml", 'w') if not os.path.exists('values-ko-rKR'): os.mkdir('values-ko-rKR') f4=file("values-ko-rKR/strings.xml", 'w') for line in file('values/strings.xml'): if '<string' in line: pos0=line.find('name="') pos1=line.find('"', pos0+6) pos2=line.find('>', pos1) pos3=line.find('</string>', pos2) val=line[pos2+1:pos3].decode('utf8').encode("gb2312") if "," in val: val='"%s"'%val key=line[pos0+6:pos1] if key not in csvmap: continue row=csvmap[key] # 'zh-rCN': '', 'en': '', 'pt-pt': '', 'zh-tw': '', 'ko-kr': if len(row['en'])==0: continue f0.write((line[:pos2+1]+row['en']+line[pos3:])) if len(row['zh-rCN'])!=0: f1.write((line[:pos2+1]+row['zh-rCN']+line[pos3:])) if len(row['pt-pt'])!=0: f2.write((line[:pos2+1]+row['pt-pt']+line[pos3:])) if len(row['zh-tw'])!=0: f3.write((line[:pos2+1]+row['zh-tw']+line[pos3:])) if len(row['ko-kr'])!=0: f4.write((line[:pos2+1]+row['ko-kr']+line[pos3:])) else: f0.write(line) f1.write(line) f2.write(line) f3.write(line) f4.write(line) f0.close() os.rename('values/string1.xml','values/string.xml')
相关文章推荐
- 多国语言的互相转换(纯资源DLL)
- iOS_Localizable.strings与Excel互相转换
- 如何使用 Visual Basic .NET 和 ASP.NET 将数据集转换为 Excel 的电子表格 XML
- 将excel表格数据转换为xml文本数据
- JAXB XML和Bean互相转换
- XSL 语言入门2 -- 使用XSLT将XML转换为(X)HTML
- 使用python脚本将XML 电子表格转变为Excel
- PHP XML和数组互相转换
- Delphi Transform TreeNode&XMLNode,树型框与XML互相加载转换
- TestLink测试用例:Excel转换XML工具<二>实现代码
- 多国语言编程之资源转换
- Excel 时间戳和时间格式的互相转换
- Json 与 XML 的互相转换
- ASP.NET使用xslt将xml转换成Excel
- Jaxb---java跟xml互相转换
- PHP XML和数组互相转换
- go语言string、int、int64互相转换
- 实体类与xml互相转换通用
- NSDictionary,JSON和XML互相转换
- 将Excel表格多个Sheet表的数据转换为SQL存储语句