今天遇到的字符串处理的问题,记录一下方便使用 str1 = input('请输入一个字符:') #初始化字符.数字.空格.特殊字符的计数 lowercase = 0 uppercase = 0 number = 0 space = 0 other = 0 for strs in str1: #如果在字符串中有小写字母,那么小写字母的数量+1 if strs.islower(): lowercase += 1 #如果在字符串中有数字,那么数字的数量+1 elif strs.isdigit(): n
利用python过滤去没用的词语,过滤的词语存储在停用文件中. #创建停用词表 def stopwordlist(): stopwords=[line.strip() for line in open ('F:\大数据\大作业\分词后的文件\stopWord.txt','r').readlines()] return stopwords f=open(r"F:\大数据\大作业\分词后的文件\data2_xinxi.txt",'r') s=f.read() #切割文件中的字符串 zifu
在这里所作的是将所有的 Python 符号和关键字列出来,这些都是值得掌握的重点. 关键字 and del from not while as elif global or with assert else if pass yield break except import print class exec in raise continue finally is return def
如你所知,Python 具有通过列表解析将列表映射到其它列表的强大能力.这种能力同过滤机制结合使用,使列表中的有些元素被映射的同时跳过另外一些元素.过滤列表语法: [mapping-expression for element in source-list if filter-expression] 这是你所知所爱的列表解析的扩展.前三部分都是相同的:最后一部分,以 if 开头的是过滤器表达式.过滤器表达式可以是返回值为真或者假的任何表达式 (在 Python 中是几乎任何东西).任何经过滤器表
以从某文件夹过滤出py文件为例: 法1: import glob import os os.chdir(“./”) for file in glob.glob(“*.py”): print file 法2: for file in os.listdir(“./”): if file.endswith(“.py”): print file 法3: for root, dirs, files in os.walk(“./”): for file in files: if file.endswith(
#coding=utf-8print 1#初始化文件crash_log.log with open('e:/1/crash_log.log','w')as f: f.close() def fw(self): print with open('e:/1/monkey_log.txt','r')as f1 , open('e:/1/crash_log.log','a+') as f2: #设置循环读取每一行,判断过滤 while True: line=f1.readline() if '// Mo
#!/usr/bin/env python # -*- coding:utf-8 -*- import subprocess import datetime plist = [] p = subprocess.Popen('net user test1234 /domain',shell=True,stdout=subprocess.PIPE) out,err = p.communicate() for line in out.splitlines(): va = str(line) vafil