Zac*_*man 6 python windows string dos batch-file
我有一个简单的python脚本,如下所示:
import sys
lines = sys.argv[1]
for line in lines.splitlines():
print line
Run Code Online (Sandbox Code Playgroud)
我想从命令行(或.bat文件)调用它,但第一个参数可能(并且可能会)是一个包含多行的字符串.怎么做到这一点?
当然,这有效:
import sys
lines = """This is a string
It has multiple lines
there are three total"""
for line in lines.splitlines():
print line
Run Code Online (Sandbox Code Playgroud)
但我需要能够逐行处理一个参数.
编辑:这可能是一个Windows命令行问题而不是Python问题.
编辑2:感谢所有好的建议.它看起来不太可能.我不能使用另一个shell,因为我实际上试图从另一个程序调用脚本,该程序似乎在幕后使用Windows命令行.
只需将参数括在引号中即可:
$ python args.py "This is a string
> It has multiple lines
> there are three total"
This is a string
It has multiple lines
there are three total
Run Code Online (Sandbox Code Playgroud)