如何用python统计一个python创建txt文件并写入中某个中文词语出现的次数?

在 SegmentFault,学习技能、解决问题
每个月,我们帮助 1000 万的开发者解决各种各样的技术问题。并助力他们在技术能力、职业生涯、影响力上获得提升。
问题对人有帮助,内容完整,我也想知道答案
问题没有实际价值,缺少关键内容,没有改进余地
留学狗心塞啊,接触的本来就是不会的东西,一上来要求基础又那么高,assignment搞不明白。professor给了一个初始代码
想要从一个TXT文件中统计里面这个单词出现的个数,txt文件大概这样
我们是初学,没有学过什么counter啊什么的,在网上查了好多,有一种方法是比较好用,如下图
但是这样就改变了老师给我的初始代码,求大神指点怎样不改变老师给的初始代码还能计算出来?
答案对人有帮助,有参考价值
答案没帮助,是错误的答案,答非所问
preMajor=''
for major in majors:
if major==preMajor:
len(preMajor)&0:
print(preMajor,count)
preMajor=major
len(preMajor)&0:
print (preMajor,count)
分享到微博?
关闭理由:
删除理由:
忽略理由:
推广(招聘、广告、SEO 等)方面的内容
与已有问题重复(请编辑该提问指向已有相同问题)
答非所问,不符合答题要求
宜作评论而非答案
带有人身攻击、辱骂、仇恨等违反条款的内容
无法获得确切结果的问题
非开发直接相关的问题
非技术提问的讨论型问题
其他原因(请补充说明)
我要该,理由是:
在 SegmentFault,学习技能、解决问题
每个月,我们帮助 1000 万的开发者解决各种各样的技术问题。并助力他们在技术能力、职业生涯、影响力上获得提升。#!/usr/bin/env python
# encoding: utf-8
with open('a.txt', 'r') as f:
dictResult = {}
# Find the letters each line
for line in f.readlines():
listMatch = re.findall('[a-zA-Z]+', line.lower()) # remember to lower the letters
for eachLetter in listMatch:
eachLetterCount = len(re.findall(eachLetter, line.lower()))
dictResult[eachLetter] = dictResult.get(eachLetter, 0) + eachLetterCount
# Sort the result
result = sorted(dictResult.items(), key=lambda d: d[1], reverse=True)
for each in result:
print each
使用python统计出txt文档中含有某个单词的个数
自己初步想到这种程度,如果某个大牛看到,给我优化下程序,最后各种情况都能考虑到的,包括含有标点符号,特殊字符,异常处理等等。谢谢啦.....
def main():
word = ...
python统计文件中单词数
python统计指定文件中的各个单词数,主要用了正则表达式!
代码如下:
# # -*- coding: utf-8 -*-
import re, os
#re一般处理正则表达式
统计英文单词的个数的python代码
word中对于英文单词的统计也很好,大家不妨试试。如果没有安装word,而且你也是程序员的话,那么可以使用我的这段代码。通过测试,word的统计结果是18674,软件的统计结果是18349,相差不到2...
python统计文本中单词个数
#!/usr/bin/env python
file_name=&hello.txt&
line_counts=0
word_counts=0
char_counts=0
file=open...
利用python内置函数,快速统计单词在文本中出现的次数
python中包含许多标准编程数据结构,如list(列表),tuple(元组)、dict(字典)和set(),如果现有的数据类型不能满足需求,可以派生某个内置类型进行定制,或者使用collection...
Python实现计算一段文本中每个单词出现的次数
看实验楼的课程,有一个小练习,做了一下。要求用Python实现计算一段文本中每个单词出现的次数。sentence = 'hello world nihao world hey hello java w...
Python计算文件中单词个数
功能描述:
在linux命令行运行Python程序WordCount
1.如果只输入单个文件无命令则顺序输出此文件中单词个数、文本行数、字节数。
2.如果输入多个文件无命令则顺序输出这两个文件中单词个...
Spark案例:Python版统计单词个数
1、Python项目PythonSparkWordCount2、input目录里的文本文件test.txt3、创建word_count.py文件实现词频统计import os
import shuti...
Python实现统计文本当中单词的数量,
这是阿里巴巴2016年的一道面试题:
统计英文文章中单词出现的次数,并且输出出现次数的前10个单词
文本如下:
Accessing Text from the Web and from Disk
python 统计文章单词个数
代码def getText():
txt=open(&article.txt&,&r&).read()
txt=txt.lower()
for ch in '~!@#$%^&*...
没有更多推荐了,Python-统计txt文件里面的分数
时间: 22:20:33
&&&& 阅读:530
&&&& 评论:
&&&& 收藏:0
标签:&&&&&&&&&&&&&&&&&&&&&&&&&&&处理文件中的数据的应用:
比如我现在拿到一份文档,里面有某个班级里所有学生的平时作业成绩。因为每个人交作业的次数不一样,所以成绩的数目也不同,没交作业的时候就没有分。我现在需要统计每个学生的平时作业总得分。
文档里的数据:
#--&scores.txt刘备&23&35&44&47&51关羽&60&77&68张飞&97&99&89&91诸葛亮&100
纤细的过程如图:
代码如下:
# -*- coding: cp936 -*-
f = file("scores.txt")
lines = f.readlines()
#print lines
results = []
for line in lines:
#print line
data = line.split()
#print data
in data[1:]:
sum += int(score)
result = ‘%s\t :
%d\n‘ % (data[0] , sum)
#print result
results.append(result)
#print results
output = file(‘result.txt‘ , "w")
output.writelines(results)
output.close()
&当成绩不足60分时,不记入总成绩
&标签:&&&&&&&&&&&&&&&&&&&&&&&&&&&原文:http://www.cnblogs.com/Mokaffe/p/4423287.html
教程昨日排行
&&国之画&&&& &&&&&&
&& &&&&&&&&&&&&&&
鲁ICP备号-4
打开技术之扣,分享程序人生!共被编辑 2 次
如何用python对一个文件夹下的多个txt文本进行去停用词。
在用 for 循环去停用词的部分,出错,仅去掉了 stopwords 中的部分停用词,且相同停用词只去除了一次。求大神告知错误之处,贴上代码再好不过!!
#encoding=utf-8
import sys
import codecs
import shutil
import jieba
import jieba.analyse
#导入自定义词典
#jieba.load_userdict("dict_baidu.txt")
#Read file and cut
def read_file_cut():
#create path
stopwords = {}.fromkeys([ line.strip() for line in open('stopword.txt') ])
path = "Lon\\"
respath = "Lon_Result\\"
if os.path.isdir(respath):
#如果respath这个路径存在
shutil.rmtree(respath, True)
#则递归移除这个路径
os.makedirs(respath)
#重新建立一个respath目录
while num&=20:
name = "%d" % num
fileName = path + str(name) + ".txt"
resName = respath + str(name) + ".txt"
source = open(fileName, 'r')
if os.path.exists(resName):
os.remove(resName)
result = codecs.open(resName, 'w', 'utf-8')
line = source.readline()
line = line.rstrip('\n')
while line!="":
line = unicode(line, "utf-8")
seglist = jieba.cut(line,cut_all=False)
for seg in seglist:
seg=seg.encode('utf-8')
if seg not in stopwords:
output+=seg
output = ' '.join(list(seglist))#空格拼接
print output
result.write(output + '\r\n')
line = source.readline()
print 'End file: ' + str(num)
source.close()
result.close()
num = num + 1
print 'End All'
#Run function
if __name__ == '__main__':
read_file_cut()
如何用python对一个文件夹下的多个txt文本进行去停用词。
在用for 循环去停用词的部分,出错,仅去掉了stopwords中的部分停用词,且相同停用词只去除了一次。求大神告知错误之处,贴上代码再好不过!!
encoding=utf-8
import sys
import codecs
import shutil
import jieba
import jieba.analyse
导入自定义词典
jieba.load_userdict("dict_baidu.txt")
Read file and cut
def read_file_cut():
#create path
stopwords = {}.fromkeys([ line.strip() for line in open('stopword.txt') ])
path = "Lon\\"
respath = "Lon_Result\\"
if os.path.isdir(respath):
#如果respath这个路径存在
shutil.rmtree(respath, True)
#则递归移除这个路径
os.makedirs(respath)
#重新建立一个respath目录
while num&=20:
name = "%d" % num
fileName = path + str(name) + ".txt"
resName = respath + str(name) + ".txt"
source = open(fileName, 'r')
if os.path.exists(resName):
os.remove(resName)
result = codecs.open(resName, 'w', 'utf-8')
line = source.readline()
line = line.rstrip('\n')
while line!="":
line = unicode(line, "utf-8")
seglist = jieba.cut(line,cut_all=False)
for seg in seglist:
seg=seg.encode('utf-8')
if seg not in stopwords:
output+=seg
output = ' '.join(list(seglist))#空格拼接
print output
result.write(output + '\r\n')
line = source.readline()
print 'End file: ' + str(num)
source.close()
result.close()
num = num + 1
print 'End All'
Run function
if name == '__main__':
read_file_cut()
我要该,理由是:Python用于统计某.txt文件中词汇出现的频率【python吧】_百度贴吧
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&签到排名:今日本吧第个签到,本吧因你更精彩,明天继续来努力!
本吧签到人数:0成为超级会员,使用一键签到本月漏签0次!成为超级会员,赠送8张补签卡连续签到:天&&累计签到:天超级会员单次开通12个月以上,赠送连续签到卡3张
关注:194,233贴子:
Python用于统计某.txt文件中词汇出现的频率收藏
51CTO学院12年行业品牌,1600万用户选择,中国专业IT技能学习平台.python资深大牛授课,0基础从入门到精通,python报名与培训中心.
需要用到自然语言处理库nltk,
词汇出现的次数, 标点符号包括在内
词汇还是字啊?
登录百度帐号}

我要回帖

更多关于 python创建txt文件并写入 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信