将多行字符串作为参数传递给 Windows 中的脚本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/749049/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 12:18:18  来源:igfitidea点击:

Passing a multi-line string as an argument to a script in Windows

pythonwindowsstringdosbatch-file

提问by Zack The Human

I have a simple python script like so:

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

import sys

lines = sys.argv[1]

for line in lines.splitlines():
    print line

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?

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

Of course, this works:

当然,这有效:

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.

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

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

编辑:这可能更像是 Windows 命令行问题而不是 Python 问题。

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.

编辑 2:感谢所有好的建议。看起来不太可能。我不能使用另一个 shell,因为我实际上试图从另一个程序调用脚本,该程序似乎在幕后使用 Windows 命令行。

回答by Dan Menes

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.

我知道这个线程已经很老了,但是我在尝试解决类似问题时遇到了它,其他人也可以,所以让我向您展示我是如何解决它的。

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

这至少适用于 Windows XP Pro,Zack 的代码位于名为
“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>

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

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

回答by moinudin

Just enclose the argument in quotes:

只需将参数括在引号中:

$ 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

回答by Joey

The following might work:

以下可能有效:

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

回答by R?mulo Ceccon

This is the only thing which worked for me:

这是唯一对我有用的东西:

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

For me Johannes' solutioninvokes the python interpreter at the end of the first line, so I don't have the chance to pass additional lines.

对我来说,Johannes 的解决方案在第一行的末尾调用 python 解释器,所以我没有机会传递额外的行。

But you said you are calling the python script from another process, not from the command line. Then why don't you use dbr' solution? This worked for me as a Ruby script:

但是你说你是从另一个进程调用 python 脚本,而不是从命令行。那你为什么不使用dbr' 解决方案呢?这对我来说是一个 Ruby 脚本:

puts `python a.py "This is a string\nIt has multiple lines\nThere are three total"`

And in what language are you writing the program which calls the python script? The issue you have is with argument passing, not with the windows shell, not with Python...

你用什么语言编写调用python脚本的程序?您遇到的问题是参数传递,而不是 Windows shell,而不是 Python ...

Finally, as mattkempsaid, I also suggest you use the standard input to read your multi-line argument, avoiding command line magic.

最后,正如mattkemp所说,我还建议您使用标准输入来读取多行参数,避免使用命令行魔法。

回答by mattkemp

Have you tried setting you multiline text as a variable and then passing the expansion of that into your script. For example:

您是否尝试将多行文本设置为变量,然后将其扩展到您的脚本中。例如:

set Text="This is a string
It has multiple lines
there are three total"
python args.py %Text%

Alternatively, instead of reading an argument you could read from standard in.

或者,您可以从标准输入中读取,而不是读取参数。

import sys

for line in iter(sys.stdin.readline, ''):
    print line

On Linux you would pipe the multiline text to the standard input of args.py.

在 Linux 上,您可以将多行文本通过管道传输到 args.py 的标准输入。

$ <command-that-produces-text> | python args.py

$ <command-that-produces-text> | 蟒蛇args.py

回答by dbr

Not sure about the Windows command-line, but would the following work?

不确定 Windows 命令行,但以下是否可行?

> python myscript.py "This is a string\nIt has multiple lines\there are three total"

..or..

..或者..

> python myscript.py "This is a string\
It has [...]\
there are [...]"

If not, I would suggest installing Cygwin and using a sane shell!

如果没有,我建议安装 Cygwin 并使用正常的外壳!