“落雨”通过精心收集,向本站投稿了8篇Python实现删除Android工程中的冗余字符串,下面就是小编给大家带来的Python实现删除Android工程中的冗余字符串,希望能帮助到大家!

Python实现删除Android工程中的冗余字符串

篇1:Python实现删除Android工程中的冗余字符串

这篇文章主要介绍了Python实现删除Android工程中的冗余字符串,本文实现的是删除Android资源(语言)国际化机制中的一些冗余字符串,需要的朋友可以参考下

Android提供了一套很方便的进行资源(语言)国际化机制,为了更好地支持多语言,很多工程的翻译往往会放到类似crowdin这样的平台上,资源是全了,但是还是会有一些问题。

哪些问题

以下使用一些语言进行举例。其中values为工程默认的资源。

1.某语言的资源和某语言限定区域的资源之间。如values-fr-rCA存在于values-fr相同的字符串,这种表现最为严重。

2.某语言的资源和默认的资源之间。values-fr存在与values相同的字符串,可能原因是由于values-fr存在未翻译字符串导致

为什么要去重

洁癖,容不下半点冗余。

解决思路

1.如果values-fr-rCA存在于values-fr相同的字符串,去除values-fr-rCA中的重复字符串,保留values-fr。这样可以保证在values-fr-rCA下也可以正确读取到资源。

2.如果values-fr存在与values相同的字符串。如去除values-fr中得重复字符串,保留values的条目。

Py脚本

代码如下:

#!/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])

如何使用

代码如下:

python removeRepeatedStrings.py your_android_project_root_dir

篇2:python 中文字符串的处理实现代码

最近更 新

巧用Python装饰器 免去调用父类构造函数的

教你如何在Django 1.6中正确使用 Signal

使用python删除nginx缓存文件示例(python

Python时间戳与时间字符串互相转换实例代

从零学python系列之数据处理编程实例(二

python调用cmd复制文件代码分享

python 正则式使用心得

从零学Python之hello world

pycharm 使用心得(四)显示行号

Python中的map、reduce和filter浅析

热 点 排 行

Python入门教程 超详细1小时学会

python 中文乱码问题深入分析

比较详细Python正则表达式操作指

Python字符串的encode与decode研

Python open读写文件实现脚本

Python enumerate遍历数组示例应

Python 深入理解yield

Python+Django在windows下的开发

python 文件和路径操作函数小结

python 字符串split的用法分享

篇3:如何删除android中的一些程序

Python实现删除Android工程中的冗余字符串

此操作有可能会对系统产生影响,建议先进行备份后再操作,

前提条件:

1、操作系统建议为Windows XP、32位Windows Vista、32位Windows 7

2、USB数据线、Desire电量高于30%、电脑系统能正确识别Desire

3、Android SDK(2.1版本下载 / 2.2版本下载)

4、良好的心理素质以及动手能力

5、已取得root权限

6、rooting文件包(立即下载)

具体操作方法如下:

1、手机连接USB线后,用音量键“下”+电源键开机,出现红色叹号后,运行rooting包的recovery-windows.bat进入绿色recovery界面(不要关闭cmd窗口)

2、另开一个cmd窗口,进入rooting文件所在目录,装载/system目录

操作代码:

adb-windows shell mount /system

3、显示系统已安装的程序

操作代码:

adb-windows shell ls /system/app/

显示结果如下:

AccountAndSyncSettings.apk

ApplicationsProvider.apk

Bluetooth.apk

...........

HtcStreamPlayer.apk

htcsettingwidgets.apk

HtcSyncwidget.apk

HtcTwitter.apk

...........

4、有两种移除程序的方式:彻底删除或者移到SD卡上

A、彻底删除,

举例,操作代码:

adb-windows shell rm /system/app/Stock.apk

adb-windows shell rm /system/app/com.htc.StockWidget.apk

B、移到SD卡的某个目录下。

举例,操作代码:

adb-windows shell mkdir /sdcard/device_files (注释:这行代码是创建文件夹)

adb-windows mv /system/app/Stock.apk /sdcard/device-files

adb-windows mv /system/app/com.htc.StockWidget.apk /sdcard/device-files

5、至此,删除“无用”app工作结束。重启手机。

篇4:Python中字符串对齐方法介绍

这篇文章主要介绍了Python中字符串对齐方法介绍,本文介绍Python字符串内置方法ljust、rjust、center的用法,需要的朋友可以参考下

目的

实现字符串的左对齐,右对齐,居中对齐,

方法

字符串内置了以下方法:其中width是指包含字符串S在内的宽度,fillchar默认是空格,也可以指定填充字符

代码如下:

string.ljust(s, width[, fillchar])

string.rjust(s, width[, fillchar])

string.center(s, width[, fillchar])

代码如下:

In [6]: a=‘Hello!‘

In [7]: print a.ljust(10,‘+‘)

Hello!++++

In [8]: print a.rjust(10,‘+‘)

++++Hello!

In [9]: print a.center(10,‘+‘)

++Hello!++

篇5:Python 字符串中的字符倒转

最近更 新

使用python Django做网页

python局部赋值的规则

python命令行参数sys.argv使用示例

写了个监控nginx进程的Python脚本

二种python发送邮件实例讲解(python发邮件

python文件读写并使用mysql批量插入示例分

基于python的汉字转GBK码实现代码

pyqt4教程之实现windows窗口小示例分享

python双向链表实现实例代码

python网络编程之UDP通信实例(含服务器端

热 点 排 行

Python入门教程 超详细1小时学会

python 中文乱码问题深入分析

比较详细Python正则表达式操作指

Python字符串的encode与decode研

Python open读写文件实现脚本

Python enumerate遍历数组示例应

Python 深入理解yield

Python+Django在windows下的开发

python 文件和路径操作函数小结

python 字符串split的用法分享

篇6:Python实现删除文件但保留指定文件

这篇文章主要介绍了Python实现删除文件但保留指定文件,本文直接给出实现代码,并同时给出代码解释,需要的朋友可以参考下

由于给客户的发布版本上客户改动了些代码和图片,我们这边给他们更新publish都是增量更新(开发提供更新指定的文件,我们提取出来给客户进行覆盖更新),但有时需要更新的文件较多导致不得不一个一个的进行查找、替换,工作量大而且容易出错,所以用python写个保留pulish后目录的指定文件、删除其他文件的功能。

代码如下:

代码如下:

import os

import os.path

def DeleteFiles(path,fileList):

for parent,dirnames,filenames in os.walk(path):

FullPathList = []

DestPathList = []

for x in fileList:

DestPath = path + x

DestPathList.append(DestPath)

for filename in filenames:

FullPath = os.path.join(parent,filename)

FullPathList.append(FullPath)

for xlist in FullPathList:

if xlist not in DestPathList:

os.remove(xlist)

代码解释:

1、for parent,dirnames,filenames in os.walk(path): 该for循环用于遍历指定path的父文件夹、文件夹名(不含目录)、文件名

2、

代码如下:

for x in fileList:

DestPath = path + x

DestPathList.append(DestPath)

该方法两个参数分别是path,filelist,

path用来指定publish文件的存放目录,例如:‘D:publish‘,filelist通过list存放你希望保留的文件及该文件路径,例如:

[r‘1.txt‘,r‘a1.txt‘],然后将path和filelist拼接起来存放到另一个列表中就是你希望保存文件的完整路径存放在DestPathList中,既[‘D:publish1.txt‘,‘D:publisha1.txt‘]

3、

代码如下:

for filename in filenames:

FullPath = os.path.join(parent,filename)

FullPathList.append(FullPath)

将目录下所有文件的绝对路径存放在列表FullPathList中

4、

代码如下:

for xlist in FullPathList:

if xlist not in DestPathList:

os.remove(xlist)

遍历FullPathList中元素跟DestPathList中元素进行比对,如果不相同则删除文件

功能虽然简单,但工作中还是比较实用的,故在此留下脚印。

篇7:Python实现统计英文单词个数及字符串分割代码

这篇文章主要介绍了Python实现统计英文单词个数及字符串分割方法,本文分别给出代码实例,需要的朋友可以参考下

字符串分割

代码如下:

str=“a|and|hello|||ab”

alist = str.split(‘|‘)

print alist

结果

代码如下:

str=“a hello{这里换成5个空格}world{这里换成3个空格}”

alist=str.split(‘ ‘)

print alist

统计英文单词的个数的python代码

代码如下:

# -*- coding: utf-8 -*-

import os,sys

info = os.getcwd #获取当前文件名称

fin = open(u‘c:/a.txt‘)

info = fin.read()

alist = info.split(‘ ‘) # 将文章按照空格划分开

fout = open(u‘c:/count.txt‘, ‘w‘)

fout.write(‘n‘.join(alist)) # 可以通过文本文件的行号同样看到效果

##fout.write(‘%s‘ % alist)

fout.close()

allen = len(alist) # 总的单词数

nulen = alist.count(‘‘) # 空格的数量

print “words‘ number is”,allen

print “null number is”,nulen

print “poor words number is”, allen-nulen # 实际的单词数目

fin.close()

篇8:Android中RemoteViews的实现

本文结合AppWidget的应用场景,分析Android中RemoteViews的内部具体实现,

从前文《Android中AppWidget的分析与应用:AppWidgetProvider》和《Android中Launcher对于AppWidget的处理的分析:AppWidgetHost角色》中得知,Android中AppWidget的图形资源是由AppWidgetProvider通过RemoteViews提供的;而显示是由AppWidgetHost通过AppWidgetHostView把RemoteView提供的内容显示在本地View上的。AppWidgetProvider和AppWidgetHostView运行在不同的程序中,而它们沟通的图形元素和点击回馈的桥梁就是RemoteViews。

下面为了行文方便和一致,把RemoteViews的内容提供方AppWidgetProvoder称作Remote端;而显示RemoteViews内容的一方AppWidgetHost称作Local端。

一、给RemoteViews提供内容——SettingsAppWidgetProvider

下图是SettingsAppWidgetProvider(位于Settings中的com.android.settings.widget包中)作为AppWidgetProvider得到update通知之后,创建RemoteViews,并把Remote端的响应Intent以及图形元素放进RemoteViews中的顺序图。

图一、为RemoteViews提供内容和侦听

图中,

1.       Settings创建RemoteViews时,把packageName和layoutId传进去并保存起来。packageName相当重要,因为这里的layoutId和各种其他资源都是相对这个程序来说的,只有通过packageName获得相应的Context,才能进而获得资源,否则其他程序是无法获得这些资源的[Seq#1]。

2.       Settings把layoutId中的viewId指示的View被点击之后获得响应的PendingIntent设置到RemoteViews中[Seq#2~ #5]。

RemoteViews创建SetOnClickPendingIntent并把id和intent传入,SetOnClickPendingIntent保存这些值;

SetOnClickPendingIntent是RemoteViews.Action的子类,通过addAction()把SetOnClickPendingIntent加入到mActions:ArrayList保存下来。

3.       Settings把layoutId中的viewId指示的View的ImageSourceID设置到RemoteViews中[Seq#6~ #10]。

RemoteViews中有很多setXYZ()的方法,用来根据不同的要设置值的类型来设置;

setXYZ()创建ReflectionAction并把viewId和value,以及“setImageResource”作为methodName传入,ReflectionAction保存这些值;

ReflectionAction是RemoteViews.Action的子类,通过addAction()把ReflectionAction加入到mActions:ArrayList保存下来。

这里描述的是一个子过程,后续会通过AppWidgetManager把这个创建好的RemoteViews放进AppWidget系统中,从而使得AppWidget的AppWidgetHost端更新显示RemoteViews里承载的内容,

Remote端设置内容的过程,只是设置这些参数,而RemoteViews也只是用不同的RemoteViews.Action保存了这些参数。下文描述内部结构。

注意:这里的参数都是在Remote端的,在RemoteContext有效。

二、RemoteViews的内部结构

下图是RemoteViews相关的类图。

图二、RemoteViews类图

RemoteViews中保存Remote端的mPackage和mLayoutId;并用mActions:ArrayList保存各种Action。

mPackage和mLayoutId是在构造RemoteViews时传进去的[上文图中的seq#1];

mActions是设置各种Remote端的响应Intent以及图形元素的时候,保存到相应的Action中,然后把Action加入到这里保存的;

mLayoutId里的各种控件通过setTextViewText()/ setImageViewResource() / setProgressBar(),等函数在remote端设置的。这些方法再调用setType() [Type可为Boolean / Byte / Short / Int/ Long / Char / String / Uri / Bitmap/ Bundle, etc]保存到ReflectionAction中。

SetOnClickPendingIntent是用来在local端用户点击viewId时,发出pendingIntent通知的。在SetOnClickPendingIntent的构造方法中保存viewId和pendingIntent。

ReflectionAction用来在local端显示时,通过Reflect机制执行获得Remote端资源的。在ReflectionAction的构造方法中保存viewId,methodName,type以及value。

ViewGroupAction和SetDrawableParameters也是RemoteViews.Action的子类,在这个场景中并未用到,基本原理相似,读者可自行分析。

三、显示RemoteViews内容——AppWidgetHostView

图一中为RemoteViews提供了内容之后,AppWidgetHost会通过IAppWidgetHost.updateAppWidget()被通知到Remote端有更新,本地端把RemoteViews提供的内容显示在AppWidgetHostView上。下面的顺序图描述这一过程。

图三、本地显示RemoteViews里的内容

图中:

1.     获取RemoteViews里Remote端(AppWidgetProvider)的packageName和layoutId,通过packageName创建远端的context——remoteContext。[Seq#1~ 6]

2.     通过RemoteViews的apply()方法,真正开始执行侦听Click操作的动作;通过远端Layout获得本地使用的View。[Seq#7~ 20]

2.1. 克隆一个本地的LayoutInflater;[Seq#8]

2.2. 用克隆出的LayoutInflater对remote端的layoutId执行Inflate,获得Layout所描述的View的Hierarchy,亦即后面用到的rootView;[Seq#9~ 10]

2.3. 对2.2中获得的view执行performApply。[Seq#11~ 19]

performApply()对所有mActions中的Action都执行apply()操作。这样,

阅读剩余 0%
本站所有文章资讯、展示的图片素材等内容均为注册用户上传(部分报媒/平媒内容转载自网络合作媒体),仅供学习参考。 用户通过本站上传、发布的任何内容的知识产权归属用户或原始著作权人所有。如有侵犯您的版权,请联系我们反馈本站将在三个工作日内改正。