我有以下声明,我似乎找不到问题:
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/