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

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在使用字符串之前使用'_'是否有问题?

最佳答案

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))

关于python - 使用%s输入/输出字符串格式,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26951720/

10-10 20:05