Python 3.x 在Python程序中读取管道输入文件的内容

Python 3.x 在Python程序中读取管道输入文件的内容,python-3.x,stdin,Python 3.x,Stdin,我必须读取通过管道传输到Python程序中的文本文件的内容。可以按如下方式将文件传递到输入文件 您的程序必须接受来自两个来源的输入:在命令行参数中传递的文件名和STDIN。例如,在Linux或OSX上,/myprogram input.txt和/myprogram

我必须读取通过管道传输到Python程序中的文本文件的内容。可以按如下方式将文件传递到输入文件

  • 您的程序必须接受来自两个来源的输入:在命令行参数中传递的文件名和STDIN。例如,在Linux或OSX上,
    /myprogram input.txt
    /myprogram
    都应该可以工作
  • 我不知道怎么做

    if __name__ == '__main__': 
      # I don't know what to do here.
    
    输入文件有以下几行,每一行都需要解析

    Add Tom 4111111111111111 $1000
    Add Lisa 5454545454545454 $3000
    Add Quincy 1234567890123456 $2000
    Charge Tom $500
    Charge Tom $800
    Charge Lisa $7
    Credit Lisa $100
    Credit Quincy $200
    
    每当我尝试
    python myprogram.py>input.txt
    ,程序就会挂起。如果有帮助的话,我正在使用Python 3.6.5

    更新:

    我尝试了以下几点:

    (env) myproject (master) $ python main.py > test.txt
    testing testing testing
    1 2 3
    1 2 3
    
    如果文件不存在,则该文件将创建一个新文件,或使用输入的内容覆盖现有文件。在本例中,使用上述内容创建了一个名为
    test.txt
    的新文件

    更新#2

    我试过这样的东西

    if __name__ == '__main__':
      for line in sys.stdin.readline():
          print (line)
    
    Add Tom 4111111111111111 $1000
    
    像这样的一行

    if __name__ == '__main__':
      for line in sys.stdin.readline():
          print (line)
    
    Add Tom 4111111111111111 $1000
    
    每个字符显示在新行中,如中所示

    A
    d
    d
    
    T
    o
    m
    
    . . .
    

    我希望所有字符都打印在一行上。

    sys.argv
    是一个列表,其中包含调用程序时使用的参数(如果有)

    如果只有一个元素长,则调用程序时没有任何参数(脚本名称本身是第一个参数,因此它实际上不起作用),因此应该从
    sys.stdin
    读取


    否则,如果它有两个或多个元素,则调用程序时会使用文件名作为参数,因此您应该打开该文件并将其用作输入。

    sys.argv
    是一个列表,其中包含调用程序时使用的参数(如果有)

    如果只有一个元素长,则调用程序时没有任何参数(脚本名称本身是第一个参数,因此它实际上不起作用),因此应该从
    sys.stdin
    读取


    否则,如果它有两个或多个元素,则会以文件名作为参数调用您的程序,因此您应该打开该文件并将其用作输入。

    这对我来说效果最好

    这将迭代sys.argv[1:]中列出的所有文件的行,如果列表为空,则默认为sys.stdin

    经过一点尝试和错误,这就是我想到的

    def handle_command_line_inputs():
        """Read the contents of the file piped through the command line.
    
        Once the operations are executed, then the balance is ready to be displayed
        """
        logging.info('Reading from the file')
        for line in fileinput.input():
            print(line)
            # My custom logic
    
    这会打印每一行

    在我的单元测试中

    from unittest import mock
    
    
    def get_test_file():
        current_path = os.path.dirname(os.path.realpath(__file__))
        return os.path.normpath(os.path.join(current_path, 'input.txt'))
    
    
    def read_input_file():
        """Simulates the generators that read file contents."""
        input_file = get_test_file()
        with open(input_file, 'r') as f:
            for line in f:
                yield line
    
    
    @mock.patch('fileinput.input', side_effect=read_input_file)
    def test_handle_command_line_inputs(mocked_fileinput):
    
        myprogram.handle_command_line_inputs()
        # Run your assertions.
    

    这对我来说效果最好

    这将迭代sys.argv[1:]中列出的所有文件的行,如果列表为空,则默认为sys.stdin

    经过一点尝试和错误,这就是我想到的

    def handle_command_line_inputs():
        """Read the contents of the file piped through the command line.
    
        Once the operations are executed, then the balance is ready to be displayed
        """
        logging.info('Reading from the file')
        for line in fileinput.input():
            print(line)
            # My custom logic
    
    这会打印每一行

    在我的单元测试中

    from unittest import mock
    
    
    def get_test_file():
        current_path = os.path.dirname(os.path.realpath(__file__))
        return os.path.normpath(os.path.join(current_path, 'input.txt'))
    
    
    def read_input_file():
        """Simulates the generators that read file contents."""
        input_file = get_test_file()
        with open(input_file, 'r') as f:
            for line in f:
                yield line
    
    
    @mock.patch('fileinput.input', side_effect=read_input_file)
    def test_handle_command_line_inputs(mocked_fileinput):
    
        myprogram.handle_command_line_inputs()
        # Run your assertions.
    

    请注意,
    在shell中有不同的含义<代码>
    重定向标准输出。通过执行
    python myprogram.py>input.txt
    ,您可以让shell将python脚本的输出重定向到文件
    input.txt
    。如果您的程序不打印任何内容,它将创建一个空文件。它还将覆盖文件的任何现有内容。
    python myprogram.pyout.txt
    读取input.txt并写入out。txt@AdamBurke我已经用我的发现更新了我的帖子。现在你已经将字符放入一个名为
    line
    的变量中。。。在将想要输出的内容传递到
    print()
    之前,您必须对它们进行处理,可能是将它们传递给函数和其他操作。接下来可能会尝试编写一个
    parse()
    函数。特别是在stdin/stdout部分,请注意它们在python中都是类似文件的对象。因此,您通常可以查看所传递的参数的数量,并使用这些参数确定是使用文件名还是stdin,然后将其传递,就像@john gordon saysNote所说的那样,
    在shell中有不同的含义<代码>重定向标准输出。通过执行
    python myprogram.py>input.txt
    ,您可以让shell将python脚本的输出重定向到文件
    input.txt
    。如果您的程序不打印任何内容,它将创建一个空文件。它还将覆盖文件的任何现有内容。
    python myprogram.pyout.txt
    读取input.txt并写入out。txt@AdamBurke我已经用我的发现更新了我的帖子。现在你已经将字符放入一个名为
    line
    的变量中。。。在将想要输出的内容传递到
    print()
    之前,您必须对它们进行处理,可能是将它们传递给函数和其他操作。接下来可能会尝试编写一个
    parse()
    函数。特别是在stdin/stdout部分,请注意它们在python中都是类似文件的对象。因此,您通常可以查看传递的参数数量,并使用这些参数确定是使用文件名还是stdin,然后传递这些参数,就像@johngordon所说的那样