读取文件,每一行做字符的截取,然后判断条件重新写入到新的txt文件:
def openreadtxt(file_name):
packageList=['com.haier.uhome.washer', 'com.tencent.mm', 'com.ishansong', 'com.midea.vm.washer', 'com.didapinche.booking', 'cn.yonghui.hyd', 'com.jingantech.iam.mfa.android.app', 'make.more.r2d2.cellular_z', 'com.ccb.longjiLife', 'com.wudaokou.hippo', 'com.gotokeep.keep', 'com.sankuai.meituan.dispatch.homebrew', 'com.sdu.didi.psnger', 'com.sankuai.meituan', 'com.cgbchina.xpt', 'com.pandabus.yinchuan', 'com.ziroom.ziroomcustomer', 'com.icbc', 'com.sunboxsoft.charge.institute', 'com.free_ride.yiwei', 'com.myapp.app.xaoorti', 'com.cmcc.cmvideo', 'com.gci.zbxc.cartrain', 'com.szlanyou.nissaniov', 'com.sankuai.meituan.takeoutnew', 'com.codoon.gps', 'com.excean.splay', 'com.zeekrlife.mobile', 'com.netease.newsreader.activity', 'com.ydtx.camera', 'com.byd.aeri.caranywhere', 'com.garmin.android.apps.connectmobile', 'com.czb.charge', 'com.longfor.channelp', 'moc.nauxuoyoaixoaix.www', 'com.lalamove.huolala.client', 'com.tencent.map', 'com.nowcasting.activity', 'com.dogness.platform', 'com.cloudy.linglingbang', 'cmb.pb', 'com.tencent.mobileqq', 'com.hoperun.intelligenceportal', 'com.pupumall.customer', 'com.zkzn.zkzntravelapp', 'com.lty.zuogongjiao.app', 'com.zjwh.android_wh_physicalfitness', 'com.violetele.zdvod', 'com.geenk.zto.sys', 'app.huawei.auto', 'com.ztstech.android.znet', 'com.ygkj.chelaile.standard', 'me.gfuil.bmap', 'dji.go.v5', 'com.smxxy.sc', 'com.uroad.carclub', 'com.fenzotech.jimu', 'com.cnspeedtest.globalspeed', 'com.waimaiii.waimaiii', 'com.bigemap.earth', 'com.hanweb.android.zhejiang.activity', 'com.playgame.havefun', 'com.foxconn.iportalandroid', 'com.nio.pe.niopower', 'com.fivehundredpx.viewer.main', 'kr', 'com.android.dazhihui', 'cn.caocaokeji.user', 'com.ss.android.ugc.aweme', 'cn.gov.lottery', 'make.more.r2d2.cellular_pro', 'com.hnxywljsyxgs.qzqgdxfsh', 'com.cupidapp.live', 'com.starmoon.smfilms', 'com.taobao.trip', 'cn.nokia.speedtest5g', 'com.szchmtech.parkingfee', 'com.ganji.android.haoche_c', 'ctrip.android.view', 'com.subtitlelive.subhotl.ssive', 'air.tv.douyu.android', 'com.yuedong.sport', 'com.hh.wallpaper.a', 'com.tencent.wework', 'com.tbit.znddc', 'com.hysdkj168.zszx.green', 'com.sameal.fresh.kk', 'com.huawei.health', 'com.autonavi.amapauto', 'top.codeffect.mua', 'one.hh.oneclient', 'com.google.android.apps.maps', 'com.cainiao.wireless', 'cn.soulapp.android', 'com.teamtalk.im', 'com.coloros.assistantscreen', 'com.tencent.mtt', 'com.xtc.watch', 'com.ocrm.im', 'com.tencent.tmgp.sgame', 'com.futurefleet.pandabus.ui.dalian', 'com.kuaishou.nebula', 'com.sankuai.meituan.dispatch.crowdsource', 'com.alibaba.android.rimet', 'com.immomo.momo', 'com.microsoft.emmx', 'com.hytch.ftthemepark', 'com.isoftstone.welink.ipsa', 'cn.com.hkgt.gasapp', 'cn.xuexi.android', 'com.lenovo.club.app', 'com.netease.cloudmusic', 'com.yijiuyijiu.eshop', 'com.lingying.salesmanhelper', 'com.danlan.xiaolan', 'com.gacne.www', 'com.gree.greesmarthome', 'com.taobao.shoppingstreets', 'com.coloros.familyguard', 'com.icbc.elife', 'com.mmbox.xbrowser', 'cn.ishansong', 'com.dianping.v1', 'com.nfyg.hsbb', 'com.pingan.carowner', 'com.tongcheng.android', 'com.huaxiaozhu.rider', 'com.cictec.wisdombus.dongying', 'com.dw.btime', 'com.tm.fgbhcr.nearme.gamecenter', 'cn.ninebot.ninebot', 'com.quark.browser', 'com.fan.app', 'hoperun.dayun.app.androidn', 'com.ovital.ovitalMap', 'com.futurefleet.pandabus.ui.ha', 'com.geostar.geoartery.xietong.gdjc', 'cn.TuHu.android', 'cn.hsa.app', 'com.chinamworld.main', 'com.mcdonalds.gma.cn', 'com.heytap.accessory', 'im.xingzhe', 'com.rt.market.fresh', 'com.slkj.paotui.worker']
data = []
file = open(file_name,'r') #打开文件
file_data = file.readlines() #读取所有行
path_write=r'C:\\Users\\W9066209\\allAppType.txt'
file_write=open(path_write,'w+')
for row in file_data:
#tmp_list = row
#tmp_list = row.split('=') #按‘,'切分每行的数据
#tmp_list[-1] = tmp_list[-1].replace('\n',',') #去掉换行符
index0=row.find('{');
rowStr=row[index0:].replace('=', ':')
#print(row.find('{'))
#if rowStr.find("appType:-1") ==-1:
for packageName in packageList:
if rowStr.find(packageName) != -1:
file_write.write(rowStr)
data.append(rowStr.strip()) #将每行数据插入data中
return data
if __name__=="__main__":
data = openreadtxt('C:\\Users\\W9066209\\Downloads\\应用类型.txt')
print(data)
python对象与json的互相转换:
import json
pass_string = "{uid=1000, pkgName='com.oplus.engineernetwork', appType=-1, optimizeValue=-1, backgroundValue=0}".replace('=', ':')
#data = '{"data": {"test": 1, "hello": "' + pass_string + ' in this lifetime."}, "id": 4}'
json_data = json.dumps(pass_string)
json_data2=json.loads(json_data)
print(pass_string.get('appType'))
print(json_data2)
python正则匹配:
# coding:utf-8
#path = r"C:\\Users\\W9066209\\Downloads\\应用类型(1).txt"
import json
from pprint import pprint
string = '''1000 = {uid=1000, pkgName='com.oplus.engineernetwork', appType=-1, optimizeValue=-1, backgroundValue=0}
1001 = {uid=1001, pkgName='vendor.qti.imsrcs', appType=-1, optimizeValue=-1, backgroundValue=0}
1002 = {uid=1002, pkgName='com.android.bluetooth', appType=-1, optimizeValue=1, backgroundValue=0}
1027 = {uid=1027, pkgName='com.android.nfc', appType=-1, optimizeValue=1, backgroundValue=0}
1068 = {uid=1068, pkgName='com.android.se', appType=-1, optimizeValue=1, backgroundValue=0}
1073 = {uid=1073, pkgName='com.android.cellbroadcastservice', appType=-1, optimizeValue=-1, backgroundValue=0}
2000 = {uid=2000, pkgName='com.android.shell', appType=-1, optimizeValue=-1, backgroundValue=0}
10000 = {uid=10000, pkgName='com.google.android.overlay.gmsconfig.common', appType=-1, optimizeValue=-1, backgroundValue=0}'''
processed = re.sub(r'([A-Za-z_]+)', r'"\1"', string.replace('\n', '')).replace('=', ':').replace('"true"', 'true').replace('"false"', 'false')
pprint(json.loads(processed))
python读取txt文件,输出数组:
f=open('C:\\Users\\W9066209\\allGPSPackage.txt')
line = f.readline().strip() #读取第一行
txt=[]
txt.append(line)
while line: # 直到读取完文件
line = f.readline().strip() # 读取一行文件,包括换行符
txt.append(line)
f.close() # 关闭文件
print(txt)