将参数复制到列表中

时间:2016-03-24 12:21:54

标签: python

我正在尝试将传递给python脚本的参数复制到文件中。这是参数。

    f = open(in_file_name, 'w')
    for x in range(5, len(arg_list)):
        f.write(arg_list[x] + '\n')
    f.close()

据我所知,存在缓冲问题,我的数据输入错误。但是,我无法控制传入的内容。我试图从第5个参数开始将参数复制到文件中。

P123-ND 10Q
10Q H??C

该文件的结果如下:

P123-ND
10Q

这应该是:

#!/bin/python

class Argument_Indices:
    PRINTER_INDEX = 0
    AREA_INDEX = 1
    LABEL_INDEX = 2
    EMAIL_INDEX = 3
    RUN_TYPE_INDEX = 4

import argparse
import json
import os
from subprocess import call
import sys
from time import strftime

def _handle_args():
    ''' Setup and run argpars '''
    parser = argparse.ArgumentParser(description='Set environment variables for and to call Program')
    parser.add_argument('time_to_run', default='NOW', choices=['NOW', 'EOP'], help='when to run the report')
    parser.add_argument('arguments', nargs='+', help='the remaining command line arguments')
    return parser.parse_args()

def _proces_program(arg_list):
    time_stamp = strftime("%d_%b_%Y_%H_%M_%S")
    printer = arg_list[Argument_Indices.PRINTER_INDEX]
    area = arg_list[Argument_Indices.AREA_INDEX]
    label = arg_list[Argument_Indices.LABEL_INDEX]
    in_file_name = "/tmp/program{0}.inp".format(time_stamp)
    os.environ['INPUT_FILE'] = in_file_name

    f = open(in_file_name, 'w')
    for x in range(5, len(arg_list)):
        f.write(arg_list[x])
    f.close()

    call(['./Program.bin', printer, area, label])

    os.remove(in_file_name)

def main():   

    ''' Main Function '''

    arg_list = None
    args = _handle_args()
    if len(args.arguments) < 1:
        print('Missing name of input file')
        return -1
    with open(args.arguments[0]) as input_file:
        arg_list = json.load(input_file)

    _process_program(arg_list)

    return 0

if __name__ == '__main__':
    if main() != 0:
        print('Program run failed')
    sys.exit()

我怎能不包含不良数据?有效信息和不良信息之间的空间发生了什么?

根据要求,这是完整的程序:

 $scope.gridData.data([]);

1 个答案:

答案 0 :(得分:0)

对于您的确切情况(您收到重复参数并且中间有一些空格),这样可行:

received_param_list = ["0013","1","1","\"john.dow@gmail.com\"","1","P123-ND                                         10Q","10Q                                             H??C"]

arg_list = [i.split(" ")[0] for i in received_param_list]

last_param = received_param_list[-1].split()[-1]
if  last_param != arg_list[-1]:
    arg_list.append(last_param)

for x in range(5, len(arg_list)):
    print (arg_list[x])

虽然可能有另一种更简单的方式