Reputation:
I have an array of complex numbers. I copied that to a text file by using the following python script.
import csv
csvfile = "text_name.txt"
with open(csvfile, "w") as output:
writer = csv.writer(output, lineterminator='\n')
for val in filetomat:
writer.writerow([val])
I got the result like this-
(0.612751-0.112445j)
(0.453966+0.516774j)
(0.263492+1.02788j)
(0.223189+1.1474j)
(0.37237+0.812074j)
(0.620341+0.178921j)
How to remove the brackets for each value? I want to get result without brackets.
Upvotes: 0
Views: 224
Reputation: 202
If you want to just remove the brackets (but keep the +
and the j
), then you can format a string like this:
writer.writerow(["%f+%fj" % (d.real, d.imag)])
or if you want to keep the fields separate so they can be loaded more easily:
writer.writerow([d.real, d.imag])
The brackets just come from the default way in which complex numbers are converted into strings.
Upvotes: 2