python - 如何在 Windows 上使用 python -c?
问题描述
https://docs.python.org/3/using/cmdline.html
这是选项文档。但它没有为我提供任何有用的信息
我想以这种方式执行代码
python -c "def hello():\n print('hello world')"
错误信息
PS C:\Users\Administrator> python -c "def hello():\n print('hello world')"
File "<string>", line 1
def hello():\n print('hello world')
^
SyntaxError: unexpected character after line continuation character
它适用于Linux,但不适用于Windows。
希望大家能给个完整的固定命令~~~
使用 javascript 执行“python -c”时与这个 \n 问题相关的另一个问题
解决方案
尝试使用反引号而不是反斜杠。
错误:
PS C:\Users\me> python -c "def hello():\n print('hello world')"
File "<string>", line 1
def hello():\n print('hello world')
^
SyntaxError: unexpected character after line continuation character
PS C:\Users\me>
好的:
PS C:\Users\me> python -c "def hello():`n print('hello world')"
PS C:\Users\me>
有用:
PS C:\Users\me> python -c "def hello():`n print('hello world')`nhello()"
hello world
PS C:\Users\me>
只是回声看到它:
PS C:\Users\me> echo "def hello():`n print('hello world')`nhello()"
def hello():
print('hello world')
hello()
PS C:\Users\me>
推荐阅读
- r - 如何在 R 的 foreach 循环中使用多重插补数据集(mids)对象?
- android - Android Xamarin MenuInflater 从 v 6.0 更改为更高版本?
- c++ - C++头文件和类实现的问题
- sql - 取消透视到 Oracle 中的多个列
- reactjs - React - 从另一个孩子访问父母的孩子参考
- c# - c# Mock IConfiguration 保持为空
- typescript - 我不明白为什么我在使用 ts-jest 和 sequelize 时遇到此错误
- bash - 总结每一行的所有值,并使用 Bash 将结果写入新列
- python - Python将不同的数据叠加到单个地图中
- android - 跨平台(iOS 和 Android)聊天不适用于 chat21 SDK