2012-02-26 94 views
1

我是第一次在这里发布的新手程序员。任何建议或意见,将不胜感激!我正在开发一个项目,将test.csv的内容与ref.csv(包含3-4个字的字符串的单个列)进行比较,并根据它与最相似的test.csv中的每个字符串分配一个分数字符串在ref.csv中。我使用fuzzywuzzy字符串匹配模块来分配相似性分数。Python模块在bash中返回错误,但不是从IDLE中返回错误

下面的代码片断接受两个输入文件,将它们转换成数组,并打印出数组:

import csv 

# Load text matching module 
from fuzzywuzzy import fuzz 
from fuzzywuzzy import process 


# Import reference and test lists and assign to variables 
ref_doc = csv.reader(open('ref.csv', 'rb'), delimiter=",", quotechar='|') 
test_doc = csv.reader(open('test.csv', 'rb'), delimiter=",", quotechar='|') 

# Define the arrays into which to load these lists 
ref = [] 
test = [] 

# Assign the reference and test docs to arrays 
for row in ref_doc: 
    ref.append(row) 

for row in test_doc: 
    test.append(row) 

# Print the arrays to make sure this all worked properly 
# before we procede to run matching operations on the arrays 
print ref, "\n\n\n", test 

的问题是,该脚本按预期工作,当我在怠速运转,但回报当我打电话以下错误从庆典:

['one', 'two'] 
Traceback (most recent call last): 
    File "csvimport_orig.py", line 4, in <module> 
    from fuzzywuzzy import fuzz 
    File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/fuzzywuzzy/fuzz.py", line 32, in <module> 
    import utils 
    File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/fuzzywuzzy/utils.py", line 6, in <module> 
    table_from=string.punctuation+string.ascii_uppercase 
AttributeError: 'module' object has no attribute 'punctuation' 

有什么我需要在bash配置使其正常工作?或者是否有一些根本错误的IDLE没有抓住?为了简单起见,我不会在这段代码中调用fuzzywuzzy模块,但它在IDLE中按预期工作。

最终,我想使用pylevenshtein,但我试图查看在使用该额外时间之前,我是否对此脚本使用有价值。

在此先感谢。

回答

1

几乎可以肯定你有一个名为string.py的模块,它正在加载而不是stdlib的string.py模块。

您可以通过添加行

import string 
print string 

csvimport_orig.py计划证实了这一点。这应该显示类似

<module 'string' from '/usr/lib/python2.7/string.pyc'> 

[我在此刻的Linux操作系统,因此该位置是不同的,你应该看到通常/Library/Frameworks/etc.等价的。]什么它可能会表现出的却是

<module 'string' from 'string.py'> 

或者,而不是string.py,无论您的冲突库在哪里。重命名您的string.py模块。

+0

你叫它。我在我的脚本的同一目录中有一个string.py文件,这是我的错误消息中['one','two']来自的地方。重命名文件,现在脚本按预期工作。谢谢。 – acpigeon 2012-02-26 01:54:08