2012-02-26 99 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