‘壹’ 用python统计一段文本中单词出现的次数
python有个特别简单的方法就可以实现,直接用str的count方法就可以了,如下
‘贰’ 如何用python统计文本中重复行的数目
这个应该不难
先取出一行,存入字典key value=0
然后next 后面一行判断是不是在字典的key里,如果在key里,value+1
完了就结束了,这了字典就是结果
一开始想用用二维数组,好像不好理解,字典好处理一点
‘叁’ python 特定 行列 文本 统计 计数
#openfile
fin=open("file01.txt","r")
fout=open("file02.txt","w")
#init
count_AG=0
count_AT=0
count_AC=0
#datalines
forlineinfin:
dat_in=line.split()
ifdat_in[3]=="A"anddat_in[4]=="G":
count_AG+=1
ifdat_in[3]=="A"anddat_in[4]=="T":
count_AT+=1
ifdat_in[3]=="A"anddat_in[4]=="C":
count_AC+=1
#output
fout.write("count_AG:%s ",count_AG)
fout.write("count_AT:%s ",count_AT)
fout.write("count_AC:%s ",count_AC)
#closefile
fin.close()
fout.close()
‘肆’ Python怎么实现统计文本文件字数
#-*-coding:utf-8-*-
#把每句 的换行符没有统计
f=open('test.html','r+')
num=0
foriinf.readlines():
num=num+len(i.strip())
print(num)
‘伍’ python文本处理--统计
text=['3 ','35 ','5 ','75 ']
count=[0foriinrange(10)]
forlineintext:
line=int(line.strip())-1
index=line/10
count[index]+=1
printcount
#=>结果
[2,0,0,1,0,0,0,1,0,0]
这样正好就第一位是1-10, 第四位是31-40.。
text是你从文件中读取的内容。这个用open和readlines就可以完成了。
其中line = int(line.strip())-1是为了将每个index中的检测范围向上扩1,即本身第一位是0-9的,数字-1后,就能将1-10算入第一个中了。
‘陆’ Python 统计并输出文本文件中每行单词个数和总单词个数,即类似如下输出:
摘要 "#include
‘柒’ Python如何统计文本中各个词性的数量
如果是统计文本中某个词出现的数量就用循环遍历读取,匹配到一次,num+=1,最后print
‘捌’ 求解Python如何统计文本中各词性的数量
importre
File=open('文本.txt',encoding='utf-8')
#读取全部内容
s=File.readlines()
#转化成字符串
s=str(s)
#统计'NN'的数量
NN=len(re.findall(r"'NN'",s))
print(NN)
‘玖’ 用python 将文本中的数据读取,统计某个区间的个数,并将区间与个数存放在另一个文本里,这个要怎样实现
代码如下:
#coding=utf-8
#从文件中加载数据
defload_numbers(file):
numbers=[]
withopen(file,'r')asf:
forlineinf.readlines():
numbers.append(int(line))
returnnumbers
if__name__=='__main__':
#从文件data.txt中加载数字
numbers=load_numbers('data.txt')
#区间下限
min=10
#区间上限
max=30
#统计区间数字个数
cnt=0
forvalinnumbers:
#如果不要包含上下限,去掉=号
ifval>=minandval<=max:
cnt=cnt+1
#将结果保存到文件result.txt中
withopen('result.txt','w')asf:
f.write('[%d,%d],%d'%(min,max,cnt))
print('done.')
‘拾’ python统计文本中有多少行
写一个文本统计的脚本:计算并打印有关文本文件的统计数据,包括文件里包含多少个字符、行、单词数,以及前10个出现次数最多的单词按顺序排列
import time
keep=['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',' ','-',"'"]
stop_words=['the','and','i','to','of','a','you','my','that','in','she','he','her','his','it','be','was','had']
def normalize(s):
result=''
for c in s.lower():
if c in keep:
result+=c