I'm trying to export data in a certain format (.cube file); the file type isn't the main issue.
Now I have to print different line formats based on their line number. So far so good, I'm able to do it using the following:
if line_num == 0 or line_num == 1:
# comment line
output_file.write("%s\n" % (self.comments[line_num]))
continue
if line_num == 2:
# number of total atoms, and the origin coordinates
output_file.write("%4d %.6f %.6f %.6f\n" % (self.num_atoms, self.origin[0], self.origin[1], self.origin[2]))
continue
The above work, but I've wanted to use the '%' operator in the following manner:
if line_num == 2:
# number of total atoms, and the origin coordinates
output_file.write("%4d %.6f %.6f %.6f\n" % (self.num_atoms, self.origin)
because self.origin is a Numpy Array size 1X3.
When doing so, I get the following error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: only size-1 arrays can be converted to Python scalars
is there a way to do what I wanted instead of give every element in the array.
Thanks.
*self.origin{self.origin[0]} {self.origin[1]} {self.origin[2]}, which is what he's trying to simplify?self.origins. On other hand - they still has to have format placeholders for every element ofself.origin. It may be better to convertself.originsto str beforehand and have just two format placeholders. My advise was more general - to move away from old-style string formatting, more over they use python 3*self.origin) works in Python 3, but not in Python 2. In Python 2, you actually have no choice but to write out the entire thing (self.origin[0], self.origin[1], self.origin[2]).