Tag: python

Python – 从一串文本中计算重复的单词,并输出每个单词的出现次数

我有一个包含大量重复单词的words.txt文件。 我想sorting列表并计算每个单词出现的频率。 我理论上可以通过在Excel中对COUNTIF进行sorting和使用来实现,但是我有一个大于10K行的大文件,所以使用Excel是不现实的。 我到目前为止所做的count.py文件,我通过键入python count.py words.txt运行: import re from sys import argv from collections import Counter, defaultdict script, filename = argv cnt = Counter() txt = open(filename).read() freqword = defaultdict(list) for word, freq in Counter(txt).items(): freqword[freq].append(word) for freq in sorted(freqword): print('count {}: {}'.format(freq, sorted(freqword[freq]))) 但是,当我要教字时,这是对每个人物的回报。 我究竟做错了什么? 更新:感谢mhlester,指出我需要通过txt.split()将txt拆分为单词。 这是我更新的代码: from sys import argv from collections import […]

无法正确编码CSV文件?

我有这个确切的问题: https : //www.en.adwords-community.com/t5/Basics-for-New-Advertisers/Character-Encoding-used-by-the-editor/td-p/100244 (t1;博士:试图上传文件到谷歌,包含外国字符,他们看起来很有趣,当在Excel中打开,谷歌拒绝他们没有正确编码) 我有以下代码。 请注意,我已经尝试在http响应对象的开头添加一个字节顺序标记,并尝试将所有string编码为utf-8。 <some code where workbook is created and populated via xlwt> output = StringIO.StringIO() workbook.save(output) wb = open_workbook(file_contents=output.getvalue()) sheet = wb.sheet_by_name(spreadsheet) response = HttpResponse(content_type='text/csv') response['Content-Disposition'] = 'attachment; filename='+(account.name+'-'+spreadsheet).replace(',', '')+'.csv' response.write('\xEF\xBB\xBF') writer = csv.writer(response) for rownum in xrange(sheet.nrows): newRow = [] for s in sheet.row_values(rownum): if isinstance(s,unicode): newRow.append(s.encode("utf-8")) elif isinstance(s, […]

将这个excel数据的列副本转储到python numpy数组中最简单的方法是什么?

今天需要做一些过滤。 因为它会花费我至less一个小时在Excel中,我决定学习如何在大约30秒的Python中做到这一点。 最难的部分是粘贴从Excel中的数据列(一个string与回车分隔值?)到Python。 在matlab中我可以做这样的事情,但在Python中,我怀疑我需要也许把引号周围,并使用一个简单的\ n分析器或正则expression式? 谢谢您的帮助! import scipy import numpy from scipy import signal N=10 Fc=0.1 Fs=1.14 h=scipy.signal.firwin(N, Fc, Fs/2) x = [23.57734807 24.6558011 23.60110497 25.6801105 24.75524862 23.70055249 23.50718232 23.56906077 22.82265193 23.78563536 21.47348066 22.15359116] 我得到这个错误: 24.6558011 ^ SyntaxError: invalid syntax

在Python中创build一个简单的点图

我应该在一个语料库中计算n-gram,并创build一个点图,显示单词的等级和他们的计数,作为validationZipf定律的练习。 最终的结果应该是这样的: 我使用nltk来提取分布(这里仅用于unigrams): import nltk with open(r'./1.txt', 'r') as file: text = file.read() file.close() tokens = nltk.word_tokenize(text) tokens = [token.lower() for token in tokens if len(token) > 1] fdist = nltk.FreqDist(tokens) ranks = fdist.most_common() 这给了我一个所有的单词和他们的计数从最常见到最less的2元组长列表。 我想知道如何从这里出发。 我只需要在双轴飞机上绘制它。 我没有安装matpotlib / numpy,在这些库中没有任何经验。 不过,我有Microsoft Excel,所以我想知道如果我能以某种可读的格式导出这些数据并将其绘制在那里。

Python标准偏差检查

我写了一个python代码来计算一个数字列表的标准偏差。 我检查了我的答案在Excel中,它似乎是closures的。 我不知道我是否错过了一个步骤,或者我应该担心,但是如果有人有时间审查代码,看看他们是否注意到错误,请告诉我。 谢谢。 city_population = [2123,1284,7031,30788,147,2217,10000] mean = sum(city_population,0.0)/len(city_population) def stdev(city_population): length = len(city_population) total_sum = 0 for i in range(length): total_sum += pow((city_population[i]-mean),2) result = (total_sum/(length-1)) return sqrt(result) stan_dev = stdev(city_population) print "The standard deviation is",(stan_dev) 输出: The standard deviation is 9443.71609738 excel: 9986.83890663

使用Python访问excel文件的有效用法

(在我提出这个问题之前,请理解我已经完成了研究,但是这只是填补我的信息漏洞) 我有一个标准的Excel电子表格.xls,其中包含一个表,其中包含以下信息: 现在我想实现的是直接将这个.xls文件转换为一个数据types,可以存储在python应用程序的内存中,这个应用程序可以像字典一样相应地访问这些信息。 我已经阅读了一些,但是我的编码经验并不是100%,因为它已经有一段时间了。

使用xlwings在Python中的Excel表格中统计行数

我有一个Python脚本,它使用xlwings打开一个Excel文件,并逐行读取并处理某一列的值。 这里是for语句: for row in range(2, rownum): 我想在表单中实际包含某些东西的每一行重复这个函数。 它从2开始到“rownum”结束。 我的问题是如何自动计算行数并将该值传递给'rownum'。 我确信xlwings有办法做到这一点,但我无法弄清楚 – 可能是Autofit工具? 谢谢您的帮助!

searchstring并获取行和列的值

如何在Excel表格中search特定的string,并使用python中的xlrd获取Excel表格中特定string的行和列值? 任何人都可以帮我吗? import xlrd workbook = xlrd.open_workbook("1234.xls") worksheet = workbook.sheet_by_name('firstpage') 只有这么多我试过

以易于编辑和读取的方式导入和导出嵌套字典到Excel

我试图导出一个字典,格式为: d = {'Apple':{'Weight':12,'Colour':'red'}, 'Banana':{'Weight':11,'Colour':'yellow','Bunched':1} } 转换成以下格式的excel文件: NAME Weight Colour Bunched Apple 12 red Banana 11 yellow 1 d中不是每个键都有相同数量的键本身。 我设法得到了导入代码,但是我在导出代码时遇到了问题。 我到目前为止: import csv data = Fruits with open('fruits.csv', 'r') as f: reader = csv.reader(f) for row in reader: a = iter(row[1:]) data[row[0]] = dict(zip(a, a)) data = Vegetables with open('veg.csv', 'r') as f: reader = […]

列表中的Python字典(拉个人值)

我有一个包含字典的列表 – [{'Name of Business' : 'Amazon', 'Contact Name' : 'Jeff Bezos', 'Email' : 'Jeff@Amazon.com'}, { {'Name of Business' : 'Microsoft', 'Contact Name' : 'Bill Gates', 'Email' : 'Bill@Microsoft.com'}] 我稍后需要将每个业务名称,联系人姓名等保存到一个variables中,或者有一个简单的方法来调用它们以便重用。 我发现使用类似的东西 for x in dict_list: print x["Client"], x['Contact Name'], x['Email'] 给我打印所有的数据: 亚马逊Jeff Bezos Jeff@Amazon.com 微软比尔盖茨Bill@Microsoft.com 但是,如果我只想从第一个条目的信息呢? 如果我想将每个字典条目的数据保存到一个单独的variables呢? 运用 first_entry = dict_list[0] print first_entry["Client"], first_entry['Contact Name'], […]