[英]Why I couldn't execute these examples from Python
我正在閱讀有關 timeit 的 Python 3.7 文檔,但是當我嘗試示例部分中的示例時,它會出現錯誤(見下文):
python -m timeit -s 'text = "sample string"; char = "g"' 'char in text'
我收到以下錯誤:
Traceback (most recent call last):
File "C:\Users\user\Anaconda3\lib\runpy.py", line 193, in _run_module_as_main
"__main__", mod_spec)
File "C:\Users\user\Anaconda3\lib\runpy.py", line 85, in _run_code
exec(code, run_globals)
File "C:\Users\user\Anaconda3\lib\timeit.py", line 374, in <module>
sys.exit(main())
File "C:\Users\user\Anaconda3\lib\timeit.py", line 313, in main
t = Timer(stmt, setup, timer)
File "C:\Users\user\Anaconda3\lib\timeit.py", line 109, in __init__
compile(setup, dummy_src_name, "exec")
File "<timeit-src>", line 1
'text
^
SyntaxError: EOL while scanning string literal
I am running Python 3.7.3 on Windows 7. (I tested it also on Window 10: cmd & powershell)
為什么我收到這個錯誤?
編輯:當我在 msys2 或 Linux 上執行上面的命令時,它可以正常工作。
只是引用問題。 這里同樣的問題:
python -m timeit -s 'text = "sample string"; char = "g"' 'char in text'
Traceback (most recent call last):
File "C:\Python37\lib\runpy.py", line 193, in _run_module_as_main
"__main__", mod_spec)
File "C:\Python37\lib\runpy.py", line 85, in _run_code
exec(code, run_globals)
File "C:\Python37\lib\timeit.py", line 374, in <module>
sys.exit(main())
File "C:\Python37\lib\timeit.py", line 313, in main
t = Timer(stmt, setup, timer)
File "C:\Python37\lib\timeit.py", line 109, in __init__
compile(setup, dummy_src_name, "exec")
File "<timeit-src>", line 1
'text
^
SyntaxError: EOL while scanning string literal
將其更改為:
python -m timeit -s "text = 'sample string'; char = 'g' 'char in text'"
20000000 loops, best of 5: 8.62 nsec per loop
我只是在本地運行你的代碼沒有問題:
> python -m timeit -s 'text = "sample string"; char = "g"' 'char in text'
10000000 loops, best of 5: 31.6 nsec per loop
鑒於您的錯誤消息的這一部分: 'text
,我猜當您嘗試運行該命令時,您沒有得到'char in text'
末尾的最后一個單引號。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.