argparse,python3,不能将已解析的文件用作文件

时间:2018-11-11 11:12:55

标签: python-3.x argparse

因此,我需要解析.txt文件作为脚本的参数。然后按行将其拆分,将其转换为列表,然后打印。

<div id="quote">
<div class="container">
        <h2 class="section-title">Request a Quote</h2>
        <hr align="left" width="8%" class="style-one">
        <br>
    <div><? echo $errorMessage.$successMessage; ?></div>
      <form id="quoteForm" method="post">
        <div class="row">
            <div class="col-md-6">
                <div class="form-group">
                    <input type="email" class="form-control" style="height:60px" id="email" name="email" placeholder="Your email">
                    <label class="error" id="emailError"><?php echo $emailError; ?></label>
                </div>

                <div class="form-group">
                    <input type="email" class="form-control" style="height:60px" id="emailConfirm" name="emailConfirm" placeholder="Re-type your email">
                    <label class="error" for="e-mailConfirm" id="emailconfirmError"><?php echo $emailconfirmError; ?></label>
                </div>

                 <div class="form-group">
                    <input type="name" class="form-control" id="name" style="height:60px" name="name" placeholder="Your Name">
                    <label class="error" for="name" id="nameError"><?php echo $nameError; ?></label>
                </div> 

                </div>   

            <div class="col-md-6">
                <div class="form-group">
                    <select class="form-control" id="services" name="services" style="height:60px">
                      <option value="">Select Services</option>
                      <option value="Installation">Installation</option>
                      <option value="Repair">Repair</option>
                      <option value="Service and Maintenance">Service and Maintenance</option>
                    </select>
                    <label class="error" for="services" id="servicesError"><?php echo $servicesError; ?></label>
                  </div>
              <div class="form-group">
                <textarea class="form-control" id="message" name="message" placeholder="Message" style="height: 163px;" cols="35"></textarea>
                <label class="error" for="message" id="messageError"><?php echo $messageError; ?></label>
              </div>  
             </div> 
             </div>

            <div class="form-row text-center">
                <div class="col-12">
                    <button type="submit" style="width:10rem" class="btn quoteButton pt-3 pb-3 text-align-center">Get a Quote</button>
                </div>
            </div>

    </form>

</div>
</div>

因此,它最终以控制台中的parser = argparse.ArgumentParser() parser.add_argument('textA', type=argparse.FileType('r'), nargs=1, default='textA.txt') args = parser.parse_args() textA = args.textA.read().split('\n') print(textA)

我想我只是不知道如何正确解析文件

对不起,我的英语语法

1 个答案:

答案 0 :(得分:1)

narg参数是导致问题的原因。

这是说明此参数用法的文档 https://docs.python.org/3/library/argparse.html?highlight=argparse#nargs

  1. 如果您希望此arg为可选参数且默认为'textA.txt',则在这种情况下应使用nargs="?"
  2. 如果您希望将此字段设为必填字段,请将args.textA.read()更改为args.textA[0].read(),并保留nargs=1不变