为什么我不能';我们不能从Python中执行这些示例

为什么我不能';我们不能从Python中执行这些示例,python,windows,time,Python,Windows,Time,我正在阅读关于timeit的Python 3.7文档,但是当我尝试本节中的示例时,它会出现错误(请参见下文): 我得到以下错误: 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

我正在阅读关于timeit的Python 3.7文档,但是当我尝试本节中的示例时,它会出现错误(请参见下文):

我得到以下错误:

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
回溯(最近一次呼叫最后一次):
文件“C:\Users\user\Anaconda3\lib\runpy.py”,第193行,在作为主模块的运行模块中
“\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
文件“C:\Users\user\Anaconda3\lib\runpy.py”,第85行,在运行代码中
exec(代码、运行\全局)
文件“C:\Users\user\Anaconda3\lib\timeit.py”,第374行,在
sys.exit(main())
文件“C:\Users\user\Anaconda3\lib\timeit.py”,第313行,在main中
t=计时器(stmt、设置、计时器)
文件“C:\Users\user\Anaconda3\lib\timeit.py”,第109行,在\uuu init中__
编译(设置,虚拟名称,“exec”)
文件“”,第1行
"文本"
^
SyntaxError:扫描字符串文字时下线
我正在Windows7上运行Python 3.7.3。(我也在windows10:cmd和powershell上测试了它)

为什么我会犯这样的错误


EDIT:当我在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'
10000000 loops, best of 5: 31.6 nsec per loop

考虑到错误消息的这一部分:
'text
,我猜当您尝试运行该命令时,您没有在
'char in text'

的末尾获得最后一个单引号,这可能是区域设置问题(此处仅为空格),因为在Windows中,双引号可用于指定字符串,而单引号不能。在第一个示例中,'text是第一个字符串,=是第二个字符串,等等。因此,python文档中给出的示例中有一个问题,应该报告。@iamnotathematician,在CMD中,用双引号引用字符串会转义特殊字符,但“%”除外。但与Unix不同,Windows中的shell与解析程序的命令行无关。这取决于每个单独的项目。解析
main
argv
和WINAPI
CommandLineToArgvW
的VC++规则是最常见的,这些规则将单个引号视为另一个字符。有些程序具有自定义解析,在某些情况下支持单引号字符串,例如schtasks.exe甚至python.exe。但这通常是解析的
argv
参数的内部。
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