gpt4 book ai didi

python - 如何在python中将数组保存到文本文件?

转载 作者:行者123 更新时间:2023-12-04 12:05:07 25 4
gpt4 key购买 nike

我有一个这种类型的数组:

xyz = [['nameserver','panel'], ['nameserver','panel']]

如何以这种格式将其保存到 abc.txt 文件中:
nameserver panel
nameserver panel

我在迭代每一行时尝试使用:
np.savetxt("some_i.txt",xyz[i],delimiter=',');

它显示此错误:
TypeError: Mismatch between array dtype ('<U11') and format specifier 
('%.18e')

最佳答案

这是一个可能的解决方案:

data = [['nameservers','panel'], ['nameservers','panel']]

with open("output.txt", "w") as txt_file:
for line in data:
txt_file.write(" ".join(line) + "\n") # works with any number of elements in a line

关于python - 如何在python中将数组保存到文本文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51860716/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com