将多行字符串作为参数传递给Windows中的脚本 [英] Passing a multi-line string as an argument to a script in Windows

查看:82
本文介绍了将多行字符串作为参数传递给Windows中的脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的python脚本,如下所示:

I have a simple python script like so:

import sys

lines = sys.argv[1]

for line in lines.splitlines():
    print line

我想从命令行(或.bat文件)中调用它,但是第一个参数可能是(并且可能会是)其中包含多行的字符串.如何做到这一点?

I want to call it from the command line (or a .bat file) but the first argument may (and probably will) be a string with multiple lines in it. How does one do this?

当然可以,

import sys

lines = """This is a string
It has multiple lines
there are three total"""

for line in lines.splitlines():
    print line

但是我需要能够逐行处理参数.

But I need to be able to process an argument line-by-line.

这可能是Windows命令行问题,而不是Python问题.

This is probably more of a Windows command-line problem than a Python problem.

感谢所有好的建议.看起来不可能.我不能使用其他shell,因为我实际上是试图从另一个似乎在后台使用Windows命令行的程序中调用脚本.

EDIT 2: Thanks for all of the good suggestions. It doesn't look like it's possible. I can't use another shell because I'm actually trying to invoke the script from another program which seems to use the Windows command-line behind the scenes.

推荐答案

我知道此线程很旧,但是在尝试解决类似问题时遇到了它,其他人也可能遇到,所以让我向您展示如何我解决了.

I know this thread is pretty old, but I came across it while trying to solve a similar problem, and others might as well, so let me show you how I solved it.

这至少在Windows XP Pro上有效,并且Zack的代码位于一个名为
的文件中 "C:\ Scratch \ test.py":

This works at least on Windows XP Pro, with Zack's code in a file called
"C:\Scratch\test.py":

C:\Scratch>test.py "This is a string"^
More?
More? "It has multiple lines"^
More?
More? "There are three total"
This is a string
It has multiple lines
There are three total

C:\Scratch>

比上面的Romulo解决方案更具可读性.

This is a little more readable than Romulo's solution above.

这篇关于将多行字符串作为参数传递给Windows中的脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆