簡體   English   中英

Cython - “TypeError:預期字節,找到 str”

[英]Cython - “TypeError: expected bytes, str found”

我正在嘗試加快我的“for 循環” ,其中包含另外 7 個“for 循環”。 這個想法是你可以像這個breaker("akzH6Fs0")一樣運行 function,它會破壞你在引號中寫的密碼。 可悲的是,我收到了標題中的錯誤。 我試圖沖浪,但沒有任何幫助。

我正在使用 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 的初學者。 提前致謝!

正如您的回溯指出的那樣, char*無法處理傳遞str 這是因為在 Python 3.* str實際上是一個 unicode 字符串容器。

從解釋器接受字符串 arguments 的安全方法是將它們聲明為str類型。 例如:

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

有關更多信息,請參閱https://cython.readthedocs.io/en/latest/src/tutorial/strings.html#accepting-strings-from-python-code

重要提示:據我所知,function 實際上似乎沒有取得任何進展。 只要輸入字符串僅包含您定義的字符串列表中的字符,它將(非常緩慢地)打印。 你想達到什么目的?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM