【问题标题】:backspace \n \t does not work in long string python argparse退格 \n \t 在长字符串 python argparse 中不起作用
【发布时间】:2020-04-08 08:09:23
【问题描述】:

我有以下代码,我在其中添加了一个工具的描述

usage = r"""
\n-------------------------------------------
Serial Chain Processing\n \
 \n--------------------------------------------

\nSteps. 

1. Buffers to 500 m a shapefile containing the country boundaries in EPSG: 3035 
projection.\n 
2. Clip a continental mask (DLT, GRA, or TCD), to the country boundaries (EPSG:3035)\n
3. Reproject the raster from step 2 to the National Grid using a WKT file 
with the translation parameters.\n 
4. Use the raster calculator to correct the outside areas using the 
following calculation: 

\n\tif(a==1){
    \n\t\toutput=b;
\n\t}
\n\tif(a==0){
    \n\t\toutput = a;
\n\t}
\n\tif(a==1&&IsNoData(b)){
    \n\t\toutput=0;
\n\t}
\n\tif(a==1&&b==255){
    \n\t\toutput=0;
\n}
\n\tif(a==0){
    \n\t\toutput=255;
\n\t}

\nBeing
\n\ta == Binary country boundaries raster in National projection
\n\tb == Raster from Step 3.

\n5. Check for compression, no data, pyramids and create raster attribute table,
as well as the color maps. 
"""

import glob
import subprocess
import os
import gdal 
import osr
import ogr 
import argparse
from osgeo import gdal,osr

def dir_path(string):
    if os.path.isdir(string):
        return string 
    else:
        raise NotADirectoryError(string)

def main(mask, country_bound_LAEA, country_bound_NAT):

    """Initializes the workflow."""
    pass 


if __name__ == "__main__":

    parser = argparse.ArgumentParser(description=usage)
    parser.add_argument('--mask',metavar="", type=dir_path, help = "National Product in LAEA")
    parser.add_argument('--country_bound_LAEA',metavar="", type=dir_path, help = "Country national boundaries in LAEA - Shapefile")
    parser.add_argument('--WKT_file',metavar="", type=dir_path, help = "WKT File derived from the national boundaries raster")
    parser.add_argument('--country_bound_NAT',metavar="", type=dir_path, help = "National Boundaries, binary raster dataset")
    parser.parse_args()
    #main(mask, country_bound_LAEA, country_bound_NAT)

当我运行 python -h 时,它会打印存储在 usage 中的描述,但 \n\t 不能跳到下一行和同一行中的制表符,使描述成为更清楚一点。我不知道如何解决这个问题。我认为在过去,我使用增量 + 添加了越来越多的字符串,但是这很烦人,因为描述很长。

【问题讨论】:

    标签: python string argparse


    【解决方案1】:

    来自argparse docs

    在使用""" 创建多行字符串时,您不需要任何特殊的转义字符。您也不需要在""" 前面加上r。您需要做的就是向 argparse 提供 formatter_class,如下所示:

    parser = argparse.ArgumentParser(description=usage, formatter_class=argparse.RawDescriptionHelpFormatter)
    

    这应该使它能够准确地打印出它在您的代码中的外观。

    【讨论】:

    • 谢谢!我将解决方案添加到我的代码中,并且成功了!
    猜你喜欢
    • 2023-04-07
    • 2021-06-17
    • 2016-11-18
    • 1970-01-01
    • 2016-09-17
    • 2016-05-17
    • 2011-08-18
    • 2011-09-30
    • 1970-01-01
    相关资源
    最近更新 更多