python - 将字符串和整数数组保存到python中的文本文件中

标签 python string numpy text

我有一个 python 列表

temp = [['abc.jpg', 1, 2, 3, 'xyz'], ['def.jpg', 4,5,6, 'xyz']] 

要将其保存为数组,所以我这样做:

temp = np.vstack(temp)

结果:

print(temp)
temp = [['abc.jpg', '1', '2', '3', 'xyz'], ['def.jpg', '4','5','6', 'xyz']]

它正在将整数转换为字符串。我不希望这种事发生。

我想将结果保存在文本文件中。

我尝试了以下方法:

np.savetxt("data.txt", temp)

但我收到以下错误:

TypeError: Mismatch between array dtype ('<U8') and format specifier ('%.18e %.18e %.18e %.18e %.18e %.18e')

最佳答案

试试这个(它保存用“;”分隔的每一行):

 np.savetxt("data.txt", temp, delimiter=" ", newline = "\n", fmt="%s")

关于python - 将字符串和整数数组保存到python中的文本文件中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47033153/

相关文章:

python - 使用 numpy 进行乘法累加

Python wheel force ABI 到 "none"

python - 在 SelectField 和 HiddenField 之间动态更改 WTForms 字段类型

python - 在 Python 中以编程方式查看网络上的其他计算机

javascript - 如何从 JQuery AJAX 调用返回多个值?

c# - 为什么字符串 Remove() 方法允许将字符作为参数?

java - 用参数/标记替换正则表达式字符串

python - 是否有适用于大数据的 for 循环或任何其他循环的动态代码?

python - 矩阵和数组的 NumPy 点积

python - 如何在 Python 单元测试中 stub time.sleep()