weixin_49304940 2021-04-07 10:58 采纳率: 100%
浏览 1809
已采纳

数据类型报错TypeError: can only concatenate tuple (not

标题写不下了,数据类型报错TypeError: can only concatenate tuple (not "str") to tuple

运行的时候一直报错,我不知道问题出在了哪里,可能是input_type的问题。我也不知道应该怎么解决,有人可以帮我看看吗?截取的这段代码的最后一行是出现错误的那行。用的好像是python3.8

(不知道我截的这段能看出问题吗,如果还需要看更多我再发文件或者截取更多)

    # determine which array(s) to extract from npz files
    if input_type == 'ensemble_full':
        npfile_to_extract = ('xa_ens',)
    elif input_type == 'ensemble_subsample':
        npfile_to_extract = ('xa_subsample',)
    elif input_type == ('ensemble_mean', 'ensemble_variance'):
        npfile_to_extract = ('xam', 'xav')
    elif input_type == 'ensemble_mean':
        npfile_to_extract = ('xam',)
    else:
        print('ERROR: Non-valid option specified for input data type: %s' %input_type)
        raise SystemExit(1)


    # look for files corresponding to desired input_type
      #listdirfiles = glob.glob(workdir+'/'+input_type+'_*')
    listdirfiles = glob.glob(workdir+'/ensemble_*')
    # including gmt_ensemble
    listdirfiles.extend(glob.glob(workdir+'/gmt_ensemble.npz'))
    # strip directory name, keep file name only
    listfiles = [item.split('/')[-1] for item in listdirfiles]
    # strip everything but variable name
    listvars = [(item.replace(input_type+'_','')).replace('.npz','') for item in listfiles]
  • 写回答

2条回答 默认 最新

  • 爱晚乏客游 2021-04-07 11:29
    关注

    python里面,字符串不要用逗号结尾,你用逗号结尾的,就会从字符串变成元组,也不要加括号,如果你加了括号也会被认为是元组。所以如果你能确定是字符串的话,把括号和逗号去掉试试

    npfile_to_extract = ('xa_ens',)
    print(type(npfile_to_extract))
    
    ## output:
    <class 'tuple'>
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?