首页 > 解决方案 > Unicode 上的 Python 2 字符串检查失败

问题描述

我目前正在尝试让 python 库在我的机器上工作,但由于某种原因,它的编写方式似乎被破坏了。

该库将两个 json 文件作为输入,其中一个名为library.json, in library.json,它会执行如下所示的多项检查:

def name(self, n):
        if n and isinstance(n, str):
            if re.search(r'[^a-zA-Z0-9_.]+', n):
                raise ValueError("Name can only contain alphanumeric   characters, underscores and periods. Name is: {}".format(n))
            if re.match(r'^\d', n):
                raise ValueError("Name cannot start with a number. The name is: {}".format(n))
            self._name = n
        else:
            raise ValueError('Name must be a non-empty string')

我正在使用library.json直接从文档中提取的简单内容:

{
    "word_launching_powershell": {"datetime_field": "datetime",
                                  "weight": 10,
                                  "indicators": [{"field": "process", "value": "powershell.exe"},
                                             {"field": "parent_process", "value": "winword.exe"}]

                             }
 }

还有一个简单的测试文件,同样直接取自文档:

from electus import Electus

# Create some sample data
events = [{
           "process": "winword.exe",
           "parent_process": "explorer.exe",
           "datetime": "2017-10-16T17:15:47.030114"
           },
          {"process": "powershell.exe",
           "parent_process": "winword.exe",
           "datetime": "2017-10-16T17:15:48.030123"
          }]

# The library.json file stores the indicator definitions
# The combinations of indicators are defined in jobs.json
e = Electus(library_conf='library.json', job_conf='jobs.json')

for event in events:
    alerts = e.evaluate_event(event)
    if alerts:
        print(alerts)

所以,当我运行这个测试脚本时,我得到以下信息:

Traceback (most recent call last):
  File "test_config.py", line 16, in <module>
    lib_def = LibraryDefinition(name='word_launching_powershell', definition=definition)
  File "/home/user/.local/lib/python2.7/site-packages/electus/library.py", line 17, in __init__
    self.name = name
  File "/home/user/.local/lib/python2.7/site-packages/electus/library.py", line 47, in name
    raise ValueError('Name must be a non-empty string')
ValueError: Name must be a non-empty string

做一些调试,我看到它在这里死了:

n = {unicode}u'word_launching_powershell'

所以这对我来说似乎是一个编码问题,我不明白的是,isinstance如果使用 json 文件总是会失败,为什么会用这些检查来编写整个库?我有什么办法可以修复编码吗?

标签: pythonjsonpython-2.7

解决方案


因此,我最终切换了 to 的所有引用,isinstance(n, str)并且isinstance(n, basestring)效果很好。


推荐阅读