Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
忽略python中提供的输入_Python_Shell - Fatal编程技术网

忽略python中提供的输入

忽略python中提供的输入,python,shell,Python,Shell,我有一段python代码,它调用一个shell脚本(get_list.sh),这个shell脚本调用一个.txt文件,该文件的实体如下: aaf:hfhfh:notusable:type: - city_name hhf:hgyt:usable:type: - city_name llf:hdgt:used:type: - city_name 当我在运行python代码后提供类似的输入时: 提供输入的代码: List = str(raw_input('Enter pipe separated

我有一段python代码,它调用一个shell脚本(get_list.sh),这个shell脚本调用一个.txt文件,该文件的实体如下:

aaf:hfhfh:notusable:type: - city_name
hhf:hgyt:usable:type: - city_name
llf:hdgt:used:type: - city_name
当我在运行python代码后提供类似的输入时:

提供输入的代码:

List = str(raw_input('Enter pipe separated list  : ')).upper().strip()

hhf|aaf|llf
获取输出的代码:

if List:
            try:
                    cmd = "/home/dponnura/get_list.sh -s " + "\"" + List + "\""
                    selfP = commands.getoutput(cmd).strip()

            except OSError:
                    print bcolors.FAIL + "Could not invoke Pod Details Script. " + bcolors.ENDC
它将输出显示为:

hhf detils : hfhfh:notusable:type: - city_name
aaf details : hgyt:usable:type: - city_name
llf details : hdgt:used:type: - city_name
我的要求是,如果我在执行python代码后传递输入,并且如果我的实体不在.txt文件中,那么它应该将输出显示为:

hhf detils : hfhfh:notusable:type: - city_name
aaf details : hgyt:usable:type: - city_name
llf details : hdgt:used:type: - city_name
如果我以以下方式提供输入:

hhf|aaf|llf|ggg
那么对于“ggg”,它应该向我显示:

'ggg' is wrong input
hhf detils : hfhfh:notusable:type: - city_name
aaf details : hgyt:usable:type: - city_name
llf details : hdgt:used:type: - city_name

您能告诉我如何在python或shell中执行此操作吗?

这是在python中完成的,无需调用get\u list.sh

import sys,re
List = str(raw_input('Enter pipe separated list  : ')).strip().split('|')
for linE in open(sys.argv[1]):
    for l1 in List:
        m1 = re.match(l1+':(.+)',linE)
        if m1:
            print l1,'details :',m1.group(1)
            List.remove(l1)
            break
for l1 in List : print l1,'is wrong input'
用法: python script.py textFile.txt

您的任务可以(而且我认为必须)用纯python实现。下面是其解决方案的一个可能变体,它只使用Python,而不使用外部脚本或库

pipelst = str(raw_input('Enter pipe separated list  : ')).split('|')
filepath = 'test.txt'   # specify path to your file here
for lns in open(filepath):
    split_pipe = lns.split(':', 1)
    if split_pipe[0] in pipelst:
        print split_pipe[0], ' details : ', split_pipe[1]
        pipelst.remove(split_pipe[0])
for lns in pipelst : print lns,' is wrong input'

如您所见,它简短、简单且清晰。

请将其修改为可读(尤其是使用
{}
按钮将代码缩进为代码块)。此外,您似乎缺少了大部分所需的输出(“它应该显示为:”然后什么也没有…),这使得您很难弄清楚您想要什么。请再次检查它现在是否可读。
hhf | aaf | llf
是提供输入的代码的一部分吗?如果没有,是什么?同时,您还有“…if应该将输出显示为:”后面没有任何内容。作为旁注,为什么要对
原始输入的结果调用
str
?如果不是
str
,您希望
raw\u input
为您提供什么?谢谢Abarnert hhf | aaf | llf不是代码的一部分。这是我提供的信息。if应将输出显示为:“ggg”是错误的输入hhf详细信息:HHFH:notusable:type:-城市名称aaf详细信息:hgyt:usable:type:-城市名称llf详细信息:hdgt:used:type:-城市名称如果我提供O/P:hhf | aaf | llf | ggg并且“.txt文件中不存在ggg”。我需要调用shell脚本:(Hi sidharth,我在添加上述代码时遇到以下错误:回溯(最近一次调用最后一次):文件“/main_menu.py”,第967行,在pPODName()文件中“/main_menu.py”,第900行,在pPODName中,用于打开的行(sys.argv[1]):indexer错误:列表索引超出范围您必须将textFile.txt作为脚本的参数有人能解释我的答案有什么问题以及为什么被否决吗?