1

私は初めてここに投稿する初心者プログラマーです。任意の提案やアドバイスをいただければ幸いです!たとえば、test.csvとref.csv(両方とも3〜4語の文字列を含む単一の列)の内容を比較し、test.csvの各文字列に、最も類似しているものとの類似性に基づいてスコアを割り当てるプロジェクトに取り組んでいます。 ref.csvの文字列。ファジーウージー文字列照合モジュールを使用して、類似度スコアを割り当てています。

次のコードスニペットは、2つの入力ファイルを取得し、それらを配列に変換して、配列を出力します。

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

問題は、このスクリプトをIDLEで実行すると期待どおりに機能するが、bashから呼び出すと次のエラーが返されることです。

['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を使用したいのですが、このスクリプトを使用することに価値があるかどうかを確認してから、その作業に時間をかけます。

前もって感謝します。

4

1 に答える 1

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ます。

于 2012-02-26T01:38:19.227 に答える