Skip to content

Converter give error: cannot use a string pattern on a bytes-like object #3

Description

@simongcc

There is an error like this

  File "/somewhere/miniforge3/lib/python3.10/site-packages/opencc/opencc.py", line 67, in convert
    split_string_list = self.split_chars_re.split(string)
TypeError: cannot use a string pattern on a bytes-like object

So the solution is adding a checking to the string returned by the converter like this

def s2t(text):
    if isinstance(text, bytes):
        text = text.decode('utf-8')  # decode bytes to string using utf-8 encoding
    return converter.convert(text)

Then the error will be resolved.
Tested with Simplified Chinese to Traditional Chinese

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions