调试python3中的无效utf8字符

2024-09-25 16:21:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图调试为什么python3脚本中的某些字符串具有非utf8字符。我发现这个脚本应该可以识别这样的角色:

https://rgxdb.com/r/5A2OX1FG

网站提供了python代码:

regex = r"""
    (?:
        [\xC0-\xC1] # Invalid UTF-8 Bytes
      | [\xF5-\xFF] # Invalid UTF-8 Bytes
      | \xE0[\x80-\x9F] # Overlong encoding of prior code point
      | \xF0[\x80-\x8F] # Overlong encoding of prior code point
      | [\xC2-\xDF](?![\x80-\xBF]) # Invalid UTF-8 Sequence Start
      | [\xE0-\xEF](?![\x80-\xBF]{2}) # Invalid UTF-8 Sequence Start
      | [\xF0-\xF4](?![\x80-\xBF]{3}) # Invalid UTF-8 Sequence Start
      | (?<=[\x0-\x7F\xF5-\xFF])[\x80-\xBF] # Invalid UTF-8 Sequence Middle
      | (?<![\xC2-\xDF]|[\xE0-\xEF]|[\xE0-\xEF][\x80-\xBF]|[\xF0-\xF4]|[\xF0-\xF4][\x80-\xBF]|[\xF0-\xF4][\x80-\xBF]{2})[\x80-\xBF] # Overlong Sequence
      | (?<=[\xE0-\xEF])[\x80-\xBF](?![\x80-\xBF]) # Short 3 byte sequence
      | (?<=[\xF0-\xF4])[\x80-\xBF](?![\x80-\xBF]{2}) # Short 4 byte sequence
      | (?<=[\xF0-\xF4][\x80-\xBF])[\x80-\xBF](?![\x80-\xBF]) # Short 4 byte sequence (2)
    )
    """  

def stripNonUtf8(str):
    matches = re.search(regex, str, re.VERBOSE)
    if matches:
        print ("Match was found at {start}-{end}: {match}".format(start = matches.start(), end = matches.end(), match = matches.group()))

但我得到以下错误:

Traceback (most recent call last):
  File "log2db.py", line 330, in <module>
    main()
  File "log2db.py", line 325, in main
    stripNonUtf8("aaa")
  File "log2db.py", line 38, in stripNonUtf8
    matches = re.search(regex, str, re.VERBOSE)
  File "C:\ProgramData\Anaconda3\lib\re.py", line 183, in search
    return _compile(pattern, flags).search(string)
  File "C:\ProgramData\Anaconda3\lib\re.py", line 286, in _compile
    p = sre_compile.compile(pattern, flags)
  File "C:\ProgramData\Anaconda3\lib\sre_compile.py", line 764, in compile
    p = sre_parse.parse(p, flags)
  File "C:\ProgramData\Anaconda3\lib\sre_parse.py", line 930, in parse
    p = _parse_sub(source, pattern, flags & SRE_FLAG_VERBOSE, 0)
  File "C:\ProgramData\Anaconda3\lib\sre_parse.py", line 426, in _parse_sub
    not nested and not items))
  File "C:\ProgramData\Anaconda3\lib\sre_parse.py", line 816, in _parse
    p = _parse_sub(source, state, sub_verbose, nested + 1)
  File "C:\ProgramData\Anaconda3\lib\sre_parse.py", line 426, in _parse_sub
    not nested and not items))
  File "C:\ProgramData\Anaconda3\lib\sre_parse.py", line 736, in _parse
    p = _parse_sub(source, state, verbose, nested + 1)
  File "C:\ProgramData\Anaconda3\lib\sre_parse.py", line 426, in _parse_sub
    not nested and not items))
  File "C:\ProgramData\Anaconda3\lib\sre_parse.py", line 536, in _parse
    code1 = _class_escape(source, this)
  File "C:\ProgramData\Anaconda3\lib\sre_parse.py", line 309, in _class_escape
    raise source.error("incomplete escape %s" % escape, len(escape))
re.error: incomplete escape \x0 at position 411 (line 10, column 11)

怎么回事?你知道吗


Tags: inpyreparseliblineutffile
1条回答
网友
1楼 · 发布于 2024-09-25 16:21:17

与C语言不同,Python要求一个十六进制值的字符正好有两个数字。你知道吗

请参阅String and Bytes literals的文档,其中注明:

Unlike in Standard C, exactly two hex digits are required.

因此,代码应固定为:

| (?<=[\x00-\x7F\xF5-\xFF])[\x80-\xBF] # Invalid UTF-8 Sequence Middle

此外,Python的标准re模块的功能相对有限。您可以安装regex模块(pip install regex)并执行import regex as re以绕过这些限制。你知道吗

相关问题 更多 >