In Python the following code:
data = [np.array([[1,2,3],[4,5,6]]),[7]]
with open('A:\\examplefile.txt', 'w', newline='') as file:
file.write(str([item for item in data]))
writes the nested list to multiple lines in the file, thus:
[array([[1, 2, 3],
[4, 5, 6]]), [7]]
How might I write the entire list onto on line in the file? e.g.:
[array([[1,2,3],[4,5,6]]),[7]]
Numpy formats ndarray output by using '\n' and whitespaces such that it is readable in form of a matrix. So replacing '\n' and whitespace will do the trick.
Try this,
data = [np.array([[1,2,3],[4,5,6]]),[7]]
with open('A:\\examplefile.txt', 'w', newline='') as file:
file.write(str(data).replace('\n','').replace(' ',''))
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