在Python中将多行字符串作为命令行输入(Taking multiple lines of strings as command line input in Python)
在编码竞赛中,我提供了一个初始化文件,从中读取初始参数。 但是,有时参数分布在不同的行上,如下所示:
x
a
b
(所有这些整数)
现在,当我读取line1时,我相信我会像这样存储x :
x = int(sys.argv[1])
我该如何处理其他线路? sys.argv[2]会给我第2行的下一个参数吗?
更准确地说,将使用:
par_list = sys.argv[1:].split(" ")
给我一个关于所有线路的参数列表?
In coding contests, I am supplied an initialization file from which to read initial parameters. However, sometimes the parameters are spread over separate lines as follows:
x
a
b
(All of them integers)
Now when I read line1, I believe I would store x like this:
x = int(sys.argv[1])
How do I take care of other lines? Will sys.argv[2] give me the next parameter on line 2?
More precisely, will using:
par_list = sys.argv[1:].split(" ")
give me a list of parameters on all the lines?
原文:https://stackoverflow.com/questions/24469349
2020-02-23 20:45
满意答案
这样就可以了:
>> python program.py $(cat input.txt)
示例程序:
import sys
paras = [int(x) for x in sys.argv[1:]]
print(paras)
示例输入文件:
42
-1
100
输出:
>> python program.py $(cat input.txt)
[42, -1, 100]
This will do the trick:
>> python program.py $(cat input.txt)
Sample program:
import sys
paras = [int(x) for x in sys.argv[1:]]
print(paras)
Sample input file:
42
-1
100
Output:
>> python program.py $(cat input.txt)
[42, -1, 100]
2014-06-28
相关问答
您的代码存在一些问题导致其无法正常工作。 首先,如果您正在逐行读取输入文件,则需要删除之前和之后的空格。 这意味着在.strip()之后调用.strip()方法。 您将获得ValueError因为int()无法处理非数字字符串。 >>> int('x')
Traceback (most recent call last):
File "", line 1, in
ValueError: invalid literal for int() with base 10...
要阅读用户输入,您可以尝试使用cmd模块轻松创建一个迷你命令行解释器(具有帮助文本和自动完成功能)和raw_input,以减少花哨的东西(只需从用户读取一行文本)。 命令行输入在sys.argv中。 尝试这样在你的脚本: import sys
print (sys.argv)
有两个模块用于解析命令行选项: optparse和getopt 。 如果你只想输入文件到你的脚本,看文件输入的力量。 Python库参考是你的朋友 。 To read user input you can try the ...
Char是单个字符,而String是字符列表,即[Char] 。 如果要将参数与"instructions"进行比较,则需要将其作为String并用双引号括起instructions 。 if表达式的语法是if condition then e1 else e2所以你需要在before instructions之前添加一个。 您可能希望使用getLine而不是readLn从控制台获取String 。 instructions :: IO ()
instruction = ...
start ::...
看,问题在于你的逻辑,答案总是不是一个或另一个。 使用此代码: if protocol not in ['tcp','udp']:
print "error"
sys.exit()
See, the problem is in your logic, the answer will ALWAYS be either NOT one or the other. Use this code: if protocol not in ['tcp','udp']:
print "erro...
这样的东西...... import subprocess
proc = subprocess.Popen(
'python',stdout=subprocess.PIPE,
stdin=subprocess.PIPE)
proc.stdin.write('import antigravity\n')
proc.stdin.write('print "something"\n')
proc.stdin.close()
result = proc.stdout.read()
prin...
如果您要从Python脚本运行该命令,那么您希望查看带有stdout参数的subprocess stdout 。 如果您要从单独的shell脚本运行该脚本和Python脚本,那么您希望从一个脚本管道到另一个脚本管道,然后从sys.stdin读取。 If you're going to run the command from your Python script then you want to look at subprocess with its stdout arguments. If yo...
from optparse import OptionParser
import sys,os
def command_line():
parser = OptionParser("%prog [-s] file_name")
parser.add_option("-s",dest="filename",
metavar="file_name",help="my help message") ...
这样就可以了: >> python program.py $(cat input.txt)
示例程序: import sys
paras = [int(x) for x in sys.argv[1:]]
print(paras)
示例输入文件: 42
-1
100
输出: >> python program.py $(cat input.txt)
[42, -1, 100]
This will do the trick: >> python program.py $(cat input.t...
你可以使用$ : python3 test.py $'\t'
ANSI_002dC-报价 $'string'形式的单词是专门处理的。 单词扩展为字符串,替换为ANSI C标准指定的反斜杠转义字符。 反斜杠转义序列(如果存在)按如下方式解码: \a
alert (bell)
\b
backspace
\e
\E
an escape character (not ANSI C)
\f
form feed
\n
newline
\r
carriage return
\t
horizonta...
你传递了nargs参数,而不是action="append" : parser.add_argument("infile", default=[], nargs='*')
*表示零或更多,就像在正则表达式中一样。 如果您需要至少一个,也可以使用+ 。 由于您有默认值,我假设用户不需要传递任何默认值。 You pass the nargs argument, not action="append": parser.add_argument("infile", default=[], nargs='...
相关文章
Python 字符串操作,字符串序列用于表示和存储文本,python中字符串是不可变的,一旦声明,不能
...
python2和python3的区别,1.性能 Py3.0运行 pystone benchmark的速
...
最近在研究实时日志分析,storm确实不错,以下是命令参数: storm help Syntax:
...
功能 监控dfs使用比例 监控datanode 运行情况 监控tasktracker
...
Python 编程语言具有很高的灵活性,它支持多种编程方法,包括过程化的、面向对象的和函数式的。但最重
...
初识spark-基本概念和例子 | _yiihsia[互联网后端技术] 初
...
该程序是在python2.3上完成的,python版本间有差异。 Mapper: import sys
...
字符串的格式化 在python中也有类似于c中的printf()的格式输出标记。在python中格式化
...
python的官网:http://www.python.org/ 有两个版本,就像struts1和st
...
最新问答
如果启用了复制处理程序,请确保将其置于其中一个安全角色之后。 我见过人们做的另一件事是在不同的端口上运行admin。 最好在需要auth的页面上使用SSL,这样你就不会发送明确的密码,因此管理和复制将发生在8443上,而常规查询将在8080上发生。 如果您要签署自己的证书,请查看此有用的SO页面: 如何在特定连接上使用不同的证书? I didn't know that /admin was the context for SOLR admin because /admin does not re
第一:在您的样本中,您有: 但是你在询问 //td[@class=‘CarMiniProfile-TableHeader’] (注意TableHeader中的大写'T')。 xpath区分大小写。 第二:通过查询// td [@ class ='CarMiniProfile-TableHeader'] / td,你暗示你在外部td中有一个'td'元素,而它们是兄弟姐妹。 有很多方法可以在这里获得制作和模型
这是你的答案: http://jsfiddle.net/gPsdk/40/ .preloader-container { position: absolute; top: 0px; right: 0px; bottom: 0px; left: 0px; background: #FFFFFF; z-index: 5; opacity: 1; -webkit-transition: all 500ms ease-out;
问题是,在启用Outlook库引用的情况下, olMailItem是一个保留常量,我认为当您将Dim olMailItem as Outlook.MailItem ,这不是问题,但是尝试设置变量会导致问题。 以下是完整的解释: 您已将olMailItem声明为对象变量。 在赋值语句的右侧,在将其值设置为对象的实例之前,您将引用此Object 。 这基本上是一个递归错误,因为你有对象试图自己分配自己。 还有另一个潜在的错误,如果之前已经分配了olMailItem ,这个语句会引发另一个错误(可能是
我建议使用wireshark http://www.wireshark.org/通过记录(“捕获”)设备可以看到的网络流量副本来“监听”网络上发生的对话。 当您开始捕获时,数据量似乎过大,但如果您能够发现任何看起来像您的SOAP消息的片段(应该很容易发现),那么您可以通过右键单击并选择来快速过滤到该对话'关注TCP Stream'。 然后,您可以在弹出窗口中查看您编写的SOAP服务与Silverlight客户端之间的整个对话。 如果一切正常,请关闭弹出窗口。 作为一个额外的好处,wireshar
Android默认情况下不提供TextView的合理结果。 您可以使用以下库并实现适当的aligntment。 https://github.com/navabi/JustifiedTextView Android Does not provide Justified aligntment of TextView By default. You can use following library and achieve proper aligntment. https://github.com/
你的代码适合我: class apples { public static void main(String args[]) { System.out.println("Hello World!"); } } 我将它下载到c:\ temp \ apples.java。 以下是我编译和运行的方式: C:\temp>javac -cp . apples.java C:\temp>dir apples Volume in drive C is HP_PAV
12个十六进制数字(带前导0x)表示48位。 那是256 TB的虚拟地址空间。 在AMD64上阅读wiki(我假设你在上面,对吗?)架构http://en.wikipedia.org/wiki/X86-64 12 hex digits (with leading 0x) mean 48 bits. That is 256 TB of virtual address space. Read wiki on AMD64 (I assume that you are on it, right?) ar
这将取决于你想要的。 对象有两种属性:类属性和实例属性。 类属性 类属性对于类的每个实例都是相同的对象。 class MyClass: class_attribute = [] 这里已经为类定义了MyClass.class_attribute ,您可以使用它。 如果您创建MyClass实例,则每个实例都可以访问相同的class_attribute 。 实例属性 instance属性仅在创建实例时可用,并且对于类的每个实例都是唯一的。 您只能在实例上使用它们。 在方法__init__中定