I have a array of this type:
xyz = [['nameserver','panel'], ['nameserver','panel']]
How can I save this to an abc.txt file in this format:
nameserver panel
nameserver panel
I tried this using, on iterating over each row:
np.savetxt("some_i.txt",xyz[i],delimiter=',');
It's showing this error:
TypeError: Mismatch between array dtype ('<U11') and format specifier
('%.18e')
Let us see how to save a numpy array to a text file. Creating a text file using the in-built open() function and then converting the array into string and writing it into the text file using the write() function. Finally closing the file using close() function.
To write to a text file in Python, you follow these steps: First, open the text file for writing (or append) using the open() function. Second, write to the text file using the write() or writelines() method. Third, close the file using the close() method.
This is a possible solution:
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
Probably the simplest method is to use the json module, and convert the array to list in one step:
import json
with open('output.txt', 'w') as filehandle:
json.dump(array.toList(), filehandle)
Using the json format allows interoperability between many different systems.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With