簡體   English   中英

轉換為argparse時,字符串索引超出范圍

[英]String index out of range when converting to argparse

我從optparse更改為argparse,但是當我嘗試運行它時,我收到以下錯誤:

    if not option_string[0] in self.prefix_chars:
IndexError: string index out of range

我的代碼是:

usage = "%prog -f <fasta TFs> -a <database all> -s <database small> -d <pfam database> [options]"
version = "1.0.1"
description = " "
epilog = " "\
         " "
parser = argparse.ArgumentParser(usage=usage, description=description,
                      version="%prog "+version, epilog=epilog)

# options for running the program
# TF file
parser.add_argument("-f", "",  dest="TF", metavar="<file>",
                        help="input file with transcription factors")
parser.set_defaults(fasta=None)

我無法找到此錯誤的來源,如何解決這個問題?

在argparse中,您無法將空參數字符串傳遞給add_argument。 argparse試圖在您傳遞的空字符串(“”)中找到有效的prefix_char(例如“ - ”或“ - ”),從而導致錯誤。 試試這個:

parser.add_argument("-f",  dest="TF", metavar="<file>",
                    help="input file with transcription factors")

獲取此錯誤的唯一方法是請求不存在的索引 - 在這種情況下, option_string必須為空。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM