【问题标题】:Cython - "TypeError: expected bytes, str found"Cython -“类型错误:预期字节,找到 str”
【发布时间】:2020-05-13 03:44:36
【问题描述】:

我正在尝试加快我的 “for 循环”,其中包含另外 7 个“for 循环”。这个想法是你可以像breaker("akzH6Fs0") 这样运行函数,它会破坏你在引号中写的密码。可悲的是,我收到了标题中的错误。我试图冲浪,但没有任何帮助。

我使用的是 python 3.8.2 版本。

代码如下:

import time

cpdef breaker(char *a):
       strings = list("abcčćdđefghijklmnoprsštuvzžqwxy1234567890ABCČĆDĐEFGHIJKLMNOPRSŠTUVZŽQWXY")
       cdef char string1
       cdef char string2
       cdef char string3
       cdef char string4
       cdef char string5
       cdef char string6
       cdef char string7
       cdef char string8
       for string1 in strings:
              for string2 in strings:
                     for string3 in strings:
                            for string4 in strings:
                                   for string5 in strings:
                                          for string6 in strings:
                                                 for string7 in strings:
                                                        for string8 in strings:
                                                               if a == string1 + string2 + string3 + string4 + string5 + string6 + string7 + string8:
                                                                      password = string1 + string2 + string3 + string4 + string5 + string6 + string7 + string8
                                                                      time.sleep(1)
                                                                      print("your password is", password)
                                                                      time.sleep(10)
                                                                      quit()

                                                               print(string1 + string2 + string3 + string4 + string5 + string6 + string7 + string8)



这是整个回溯:

Traceback (most recent call last):
  File "C:\Users\korisnik\Desktop\edps_tests\testing.py", line 3, in <module>
    edps_test.breaker("password")
  File "edps_test.pyx", line 3, in edps_test.breaker
    cpdef breaker(char *a):
TypeError: expected bytes, str found
[Finished in 0.2s with exit code 1]
[shell_cmd: python -u "C:\Users\korisnik\Desktop\edps_tests\testing.py"]
[dir: C:\Users\korisnik\Desktop\edps_tests]
[path: C:\Program Files (x86)\Common Files\Oracle\Java\javapath;C:\Program Files (x86)\Common Files\Intel\Shared Libraries\redist\intel64_win\compiler;C:\ProgramData\Oracle\Java\javapath;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\WINDOWS\System32\OpenSSH\;C:\Program Files\dotnet\;D:\webdrivers;C:\Users\korisnik\AppData\Local\Programs\Python\Python38-32\Scripts\;C:\Users\korisnik\AppData\Local\Programs\Python\Python38-32\;C:\Users\korisnik\AppData\Local\Microsoft\WindowsApps;C:\Users\korisnik\AppData\Local\Microsoft\WindowsApps;C:\Program Files\JetBrains\PyCharm Community Edition 2020.1\bin;]

解决方案会很棒,但我真的很感激只是对错误和我的错误的解释。顺便说一句,我是 cython 的初学者。提前致谢!

【问题讨论】:

  • 根本问题是您正在尝试使用暴力搜索。
  • 请包含完整的回溯。
  • 无论如何,cdef char 需要字节,而不是 str AFAIKT
  • @robinsax,做到了。

标签: python cython


【解决方案1】:

正如您的回溯指出的那样,char* 无法处理传递给str 的问题。这是因为在 Python 3.* str 实际上是一个 unicode 字符串容器。

从解释器接受字符串参数的安全方法是将它们声明为str 类型。例如:

cpdef breaker(str a):
    # etc...

请参阅https://cython.readthedocs.io/en/latest/src/tutorial/strings.html#accepting-strings-from-python-code 了解更多信息。

重要提示:据我所知,这个函数实际上似乎并没有实现任何目标。只要输入字符串仅包含您定义的字符串列表中的字符,它将(非常缓慢地)打印。你想完成什么?

【讨论】:

    猜你喜欢
    • 2020-12-18
    • 2017-02-02
    • 2014-06-04
    • 2015-11-11
    • 2016-10-31
    • 2019-02-18
    • 1970-01-01
    • 1970-01-01
    • 2022-11-09
    相关资源
    最近更新 更多