【问题标题】:Python module returning errors in bash but not from IDLEPython模块在bash中返回错误但不是来自IDLE
【发布时间】:2012-02-26 01:10:41
【问题描述】:

我是第一次在这里发帖的新手程序员。任何建议或意见,将不胜感激!我正在研究一个项目,该项目将 test.csv 与 ref.csv 的内容进行比较(两个单列都包含 3-4 个单词的字符串),并根据 test.csv 中的每个字符串与最相似的字符串的相似性为每个字符串分配一个分数ref.csv 中的字符串。我正在使用fuzzywuzzy字符串匹配模块来分配相似度分数。

以下代码 sn-p 获取两个输入文件,将它们转换为数组,并打印出数组:

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 没有捕捉到一些根本性的错误?为简单起见,我没有在这个 sn-p 中调用fuzzywuzzy 模块,但它在IDLE 中按预期工作。

最后,我想使用pylevenshtein,但我想看看我对这个脚本的使用是否有价值,然后再投入额外的时间来完成这项工作。

提前致谢。

【问题讨论】:

    标签: python macos python-import python-idle fuzzywuzzy


    【解决方案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 模块。

    【讨论】:

    • 你叫它。我在与我的脚本相同的目录中有一个 string.py 文件,这是我的错误消息中的 ['one', 'two'] 的来源。重命名文件,现在脚本按预期工作。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-25
    • 2017-02-19
    • 1970-01-01
    • 2021-02-26
    • 1970-01-01
    • 2019-09-21
    相关资源
    最近更新 更多