Python - 解析来自 STDIN 的多行数据以存储在标准数组中
Python - Parsing through multiple lines of data from STDIN to store in standard array
我已经完成了建议的类似问题,但是似乎 运行 进入了死胡同;可能是因为我没有充分解释我的问题。
我正在尝试获取一些我可以访问的 STDIN,看起来像这样:
0
80
90 29
20
我遇到的问题是在第一行之后取整数并将它们全部存储到标准 Python 数组中。
[80, 90, 29, 20]
第一行的第一个输入将始终是一些整数 0 或 1,代表一些 "special feature" 的 disabling/enabling,然后输入任何其他整数,这些整数必须存储到标准大批。如您所见,一些整数的行都是它们自己,而其他行可能有几个整数(应该完全忽略空行)。
我一直在尝试使用 sys.stdin 来解决这个问题,因为我知道在剥离内容之后它已经将输入输入到列表对象中,但是收效甚微。
到目前为止我的代码如下:
parse = True
arry = []
print('Special Feature? (Give 0 to disable feature.)')
feature = input()
print('Please give the input data:')
while parse:
line = sys.stdin.readline().rstrip('\n')
if line == 'quit':
parse = False
else:
arry.append(line)
print('INPUT DATA:', arry)
'quit' 是我对后门的尝试,我可以手动输入,因为我也不知道如何检查 EOF。我知道这是非常简单的(几乎没有什么),但我实际上希望在我的输出中产生的是:
Special Feature? (Give 0 to disable feature.)
> 0
Please give the input data:
> 80 90 29 20
INPUT DATA: [80, 90, 29, 20]
没有打印标有“>”的行,我只是在演示概念上应该如何读取输入。
当然,我们将不胜感激任何帮助,期待您的想法!
您可以遍历 sys.stdin
(阅读更多 here)。
要存储您的数字,只需编写从字符串中提取数字的任何代码,然后将数字附加到列表中即可。
这是一个例子。
import sys
parse = True
arry = []
print('Special Feature? (Give 0 to disable feature.)')
feature = input()
print('Please give the input data:')
for l in sys.stdin:
arry += l.strip().split()
print('INPUT DATA:', arry)
创建一个新文件,例如data
:
0
1 2 3
4 5 6
现在尝试运行程序
$ python3 f.py < data
Special Feature? (Give 0 to disable feature.)
Please give the input data:
INPUT DATA: ['1', '2', '3', '4', '5', '6']
每个数字都是从文件中读取的。
如果你真的想保留sys.stdin
(尽管input()
),你可以使用这种方法:
import sys
parse = True
arry = []
print('Special Feature? (Give 0 to disable feature.)')
feature = input()
print('Please give the input data:')
while parse:
line = sys.stdin.readline().rstrip('\n')
if line == 'quit':
parse = False
elif line !='':
arry += [int(x) for x in line.split()]
print('INPUT DATA:', arry)
输入:
Special Feature? (Give 0 to disable feature.)
1
Please give the input data:
10
20
22
1 3 5 0
quit
输出(输入数字转换为整数):
INPUT DATA: [10, 20, 22, 1, 3, 5, 0]
我已经完成了建议的类似问题,但是似乎 运行 进入了死胡同;可能是因为我没有充分解释我的问题。 我正在尝试获取一些我可以访问的 STDIN,看起来像这样:
0
80
90 29
20
我遇到的问题是在第一行之后取整数并将它们全部存储到标准 Python 数组中。
[80, 90, 29, 20]
第一行的第一个输入将始终是一些整数 0 或 1,代表一些 "special feature" 的 disabling/enabling,然后输入任何其他整数,这些整数必须存储到标准大批。如您所见,一些整数的行都是它们自己,而其他行可能有几个整数(应该完全忽略空行)。 我一直在尝试使用 sys.stdin 来解决这个问题,因为我知道在剥离内容之后它已经将输入输入到列表对象中,但是收效甚微。 到目前为止我的代码如下:
parse = True
arry = []
print('Special Feature? (Give 0 to disable feature.)')
feature = input()
print('Please give the input data:')
while parse:
line = sys.stdin.readline().rstrip('\n')
if line == 'quit':
parse = False
else:
arry.append(line)
print('INPUT DATA:', arry)
'quit' 是我对后门的尝试,我可以手动输入,因为我也不知道如何检查 EOF。我知道这是非常简单的(几乎没有什么),但我实际上希望在我的输出中产生的是:
Special Feature? (Give 0 to disable feature.)
> 0
Please give the input data:
> 80 90 29 20
INPUT DATA: [80, 90, 29, 20]
没有打印标有“>”的行,我只是在演示概念上应该如何读取输入。 当然,我们将不胜感激任何帮助,期待您的想法!
您可以遍历 sys.stdin
(阅读更多 here)。
要存储您的数字,只需编写从字符串中提取数字的任何代码,然后将数字附加到列表中即可。
这是一个例子。
import sys
parse = True
arry = []
print('Special Feature? (Give 0 to disable feature.)')
feature = input()
print('Please give the input data:')
for l in sys.stdin:
arry += l.strip().split()
print('INPUT DATA:', arry)
创建一个新文件,例如data
:
0
1 2 3
4 5 6
现在尝试运行程序
$ python3 f.py < data
Special Feature? (Give 0 to disable feature.)
Please give the input data:
INPUT DATA: ['1', '2', '3', '4', '5', '6']
每个数字都是从文件中读取的。
如果你真的想保留sys.stdin
(尽管input()
),你可以使用这种方法:
import sys
parse = True
arry = []
print('Special Feature? (Give 0 to disable feature.)')
feature = input()
print('Please give the input data:')
while parse:
line = sys.stdin.readline().rstrip('\n')
if line == 'quit':
parse = False
elif line !='':
arry += [int(x) for x in line.split()]
print('INPUT DATA:', arry)
输入:
Special Feature? (Give 0 to disable feature.)
1
Please give the input data:
10
20
22
1 3 5 0
quit
输出(输入数字转换为整数):
INPUT DATA: [10, 20, 22, 1, 3, 5, 0]