最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python實(shí)現(xiàn)刪除Android工程中的冗余字符串

 更新時(shí)間:2015年01月19日 10:04:13   投稿:junjie  
這篇文章主要介紹了Python實(shí)現(xiàn)刪除Android工程中的冗余字符串,本文實(shí)現(xiàn)的是刪除Android資源(語言)國際化機(jī)制中的一些冗余字符串,需要的朋友可以參考下

Android提供了一套很方便的進(jìn)行資源(語言)國際化機(jī)制,為了更好地支持多語言,很多工程的翻譯往往會(huì)放到類似crowdin這樣的平臺(tái)上。資源是全了,但是還是會(huì)有一些問題。

哪些問題

以下使用一些語言進(jìn)行舉例。其中values為工程默認(rèn)的資源。

1.某語言的資源和某語言限定區(qū)域的資源之間。如values-fr-rCA存在于values-fr相同的字符串,這種表現(xiàn)最為嚴(yán)重。
2.某語言的資源和默認(rèn)的資源之間。values-fr存在與values相同的字符串,可能原因是由于values-fr存在未翻譯字符串導(dǎo)致

為什么要去重

潔癖,容不下半點(diǎn)冗余。

解決思路

1.如果values-fr-rCA存在于values-fr相同的字符串,去除values-fr-rCA中的重復(fù)字符串,保留values-fr。這樣可以保證在values-fr-rCA下也可以正確讀取到資源。

2.如果values-fr存在與values相同的字符串。如去除values-fr中得重復(fù)字符串,保留values的條目。

Py腳本

復(fù)制代碼 代碼如下:

#!/usr/bin/env python
# coding=utf-8
from os import listdir,path, system
from sys import argv
try:
    import xml.etree.cElementTree as ET
except ImportError:
    import xml.etree.ElementTree as ET


def genRegionLangPair(filePath):
    basicLanguage = None
    if ('values' in filePath) :
        hasRegionLimit = ('r' == filePath[-3:-2])
        if (hasRegionLimit):
            basicLanguage = filePath[0:-4]
            if (not path.exists(basicLanguage)) :
                return None
            belongsToEnglish =  ("values-en" in basicLanguage)
            if (belongsToEnglish):
                #Compare with the res/values/strings.xml
                return (path.dirname(basicLanguage) + '/values/strings.xml', filePath + "/strings.xml")
            else:
                return (basicLanguage + '/strings.xml', filePath + "/strings.xml")
    return None

def genLangPair(filePath):
    def shouldGenLanPair(filePath):
        if (not 'values' in filePath ):
            return False
        if('dpi' in filePath):
            return False
        if ('dimes' in filePath):
            return False
        if ('large' in filePath):
            return False
        return True

    if(shouldGenLanPair(filePath)):
        basicLanguage = path.dirname(filePath) + '/values/strings.xml'
        targetLanguage = filePath + '/strings.xml'
        if (not path.exists(targetLanguage)):
           return None

        if (not path.samefile(basicLanguage,targetLanguage)) :
            return (basicLanguage, targetLanguage)
    return None

def genCompareList(filePath):
    compareLists = []
    for file in listdir(filePath):
        regionPair = genRegionLangPair(filePath + '/' + file)
        if (None != regionPair):
            compareLists.append(regionPair)

        languagePair = genLangPair(filePath + '/' + file)
        if (None != languagePair) :
            compareLists.append(languagePair)

    return compareLists

def getXmlEntries(filePath):
    root = ET.ElementTree(file=filePath).getroot()
    entries = {}
    for child in root:
        attrib = child.attrib
        if (None != attrib) :
            entries[attrib.get('name')] = child.text
    print 'xmlEntriesCount',len(entries)
    return entries

def rewriteRegionFile(sourceEntries, filePath):
    if (not path.exists(filePath)):
        return
    ET.register_namespace('xliff',"urn:oasis:names:tc:xliff:document:1.2")
    tree = ET.ElementTree(file=filePath)
    root = tree.getroot()
    print root
    totalCount = 0
    removeCount = 0
    unRemoveCount = 0
    print len(root)
    toRemoveList = []
    for child in root:
        totalCount = totalCount + 1
        attrib = child.attrib
        if (None == attrib):
            continue

        childName = attrib.get('name')

        if (sourceEntries.get(childName) == child.text):
            removeCount = removeCount + 1
            toRemoveList.append(child)
        else:
            unRemoveCount = unRemoveCount + 1
            print childName, sourceEntries.get(childName), child.text
    print filePath,totalCount, removeCount,unRemoveCount

    for aItem in toRemoveList:
        root.remove(aItem)

    if (len(root) != 0 ):
        tree.write(filePath, encoding="UTF-8")
    else:
        command = 'rm -rf %s'%(path.dirname(filePath))
        print command
        system(command)

def main(projectDir):
    lists = genCompareList(projectDir + "/res/")

    for item in lists:
        print item
        src = item[0]
        dest = item[1]
        rewriteRegionFile(getXmlEntries(src),dest)

if __name__ == "__main__":
    if (len(argv) == 2) :
        main(argv[1])

如何使用

復(fù)制代碼 代碼如下:

python removeRepeatedStrings.py your_android_project_root_dir

相關(guān)文章

  • Python操作MongoDB的教程詳解(插,查,改,排,刪)

    Python操作MongoDB的教程詳解(插,查,改,排,刪)

    MongoDB是一個(gè)基于分布式文件存儲(chǔ)的數(shù)據(jù)庫。是一個(gè)介于關(guān)系數(shù)據(jù)庫和非關(guān)系數(shù)據(jù)庫之間的產(chǎn)品,是非關(guān)系數(shù)據(jù)庫當(dāng)中功能最豐富,最像關(guān)系數(shù)據(jù)庫的。本文將詳細(xì)和大家聊聊Python操作MongoDB的方法,需要的可以參考一下
    2022-09-09
  • 使用Tensorflow?hub完成目標(biāo)檢測過程詳解

    使用Tensorflow?hub完成目標(biāo)檢測過程詳解

    這篇文章主要為大家介紹了使用Tensorflow?hub完成目標(biāo)檢測過程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-04-04
  • python中列表的常見操作梳理總結(jié)(二)

    python中列表的常見操作梳理總結(jié)(二)

    這篇文章主要介紹了python中列表的常見操作總結(jié),文章圍通過列表的索引與切片的相關(guān)資料展開全文詳細(xì)的內(nèi)容,具有一定的參考價(jià)值,需要的小伙伴可以參考一下
    2022-07-07
  • pandas表連接 索引上的合并方法

    pandas表連接 索引上的合并方法

    今天小編就為大家分享一篇pandas表連接 索引上的合并方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-06-06
  • 零基礎(chǔ)寫python爬蟲之抓取百度貼吧并存儲(chǔ)到本地txt文件改進(jìn)版

    零基礎(chǔ)寫python爬蟲之抓取百度貼吧并存儲(chǔ)到本地txt文件改進(jìn)版

    前面已經(jīng)發(fā)了一篇關(guān)于百度貼吧抓取的代碼,今天我們來看下代碼的改進(jìn)版,參考了上篇抓取糗事百科的思路,給需要的小伙伴們參考下吧
    2014-11-11
  • Python報(bào)錯(cuò)TypeError: object of type ‘generator‘ has no len ()的解決方法

    Python報(bào)錯(cuò)TypeError: object of type ‘gener

    在Python開發(fā)的復(fù)雜世界中,報(bào)錯(cuò)信息就像神秘的謎題,困擾著開發(fā)者和環(huán)境配置者,其中,TypeError: object of type ‘generator’ has no len()這個(gè)報(bào)錯(cuò),常常在不經(jīng)意間打亂我們的開發(fā)節(jié)奏,本文讓我們一起深入探究這個(gè)報(bào)錯(cuò)問題,為Python開發(fā)之路掃除障礙
    2024-10-10
  • python單向循環(huán)鏈表原理與實(shí)現(xiàn)方法示例

    python單向循環(huán)鏈表原理與實(shí)現(xiàn)方法示例

    這篇文章主要介紹了python單向循環(huán)鏈表原理與實(shí)現(xiàn)方法,結(jié)合實(shí)例形式詳細(xì)分析了Python單向循環(huán)鏈表概念、原理、定義及使用方法,需要的朋友可以參考下
    2019-12-12
  • python利用pandas分析學(xué)生期末成績實(shí)例代碼

    python利用pandas分析學(xué)生期末成績實(shí)例代碼

    pandas是數(shù)據(jù)分析師最常用的工具之一,這篇文章主要給大家介紹了關(guān)于python如何利用pandas分析學(xué)生期末成績的相關(guān)資料,文中給出了詳細(xì)的實(shí)現(xiàn)方法,需要的朋友可以參考下
    2021-07-07
  • Python算法之求n個(gè)節(jié)點(diǎn)不同二叉樹個(gè)數(shù)

    Python算法之求n個(gè)節(jié)點(diǎn)不同二叉樹個(gè)數(shù)

    本文先向大家分享了建立二叉樹的簡單代碼,其次介紹了Python計(jì)算n個(gè)節(jié)點(diǎn)不同二叉樹個(gè)數(shù)的問題及實(shí)現(xiàn)代碼示例,具有一定參考價(jià)值,需要的朋友可以了解下。
    2017-10-10
  • 提升python處理速度原理及方法實(shí)例

    提升python處理速度原理及方法實(shí)例

    這篇文章主要介紹了提升python處理速度原理及方法實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-12-12

最新評(píng)論

上虞市| 伊川县| 富阳市| 衡阳市| 泸水县| 孟村| 祁东县| 泰和县| 灵石县| 达尔| 嵊州市| 尚志市| 文安县| 基隆市| 钟山县| 伊金霍洛旗| 阜宁县| 屏东市| 阳谷县| 汶上县| 北票市| 丹巴县| 河东区| 南丹县| 金堂县| 密山市| 卫辉市| 岳普湖县| 阳东县| 盖州市| 武穴市| 衡阳市| 佛冈县| 阳山县| 开阳县| 临猗县| 承德市| 广宗县| 新蔡县| 瑞昌市| 芜湖县|