字符串格式化输入/输出使用%s

时间:2014-11-15 22:55:43

标签: python string numpy scipy

我有以下陈述,我似乎无法找到问题:

Analysis = 'Trythis'
TestName = 'ThisOne'
NumberIteration = 25

for num in range(NumberIteration):
    x= np.loadtxt("%s/results/data_%s/Outputs/$s/%sLCOE.txt" % (Analysis, num , TestName, TestName))   

我一直收到以下错误:

  np.loadtxt("%s/results/data_%s/Outputs/$s/%sLCOE.txt" % (Analysis, TestName, TestName, TestName)) 
TypeError: not all arguments converted during string formatting

我尝试使用%d%c代替%s。使用' _' numpy是否有问题?在使用字符串之前?

1 个答案:

答案 0 :(得分:2)

Python抱怨你给%提供了四个参数,但格式字符串中只有三个%s

我认为$s应该是%s

x= np.loadtxt("%s/results/data_%s/Outputs/%s/%sLCOE.txt" % (Analysis, num, TestName, TestName))
#                                         ^^

但请注意,在现代Python代码中,您应该使用str.format代替:

x= np.loadtxt("{}/results/data_{}/Outputs/{}/{}LCOE.txt".format(Analysis, num, TestName, TestName))