python - 使用 %s 格式化输入/输出

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

最佳答案

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/

相关文章:

python - 在 Python 中使用内置 str() 类型的潜在异常

c# - 是否可以可靠地将用户文件自动解码为 Unicode? [C#]

c++ - 如何在 C++ 中将 getline() 拆分为数组

python - 使用 Numpy 数组、元组和省略号进行 bool 索引

java - 尝试使用 thrift 的 TFileTransport 和 TFileProcessor 让 Python 客户端与 Java 服务器对话

python - 如何计算数据框中每篇文章的段落数?

python - 如何根据参数存在更改 argparse 选项操作与类型行为?

python - 无法在 DataFrame 中添加列名称

python - numpy中基础的变化

python - 在 Mac 上使用 anaconda 安装 pygame