Hi I'm trying to convert elements in an np array to string so that I can output them
with open("multiplication.txt", 'w') as output:
with open("data0.txt") as orig:
lines = orig.readlines()
for line in lines: # one line
b = np.fromstring(line, dtype=float, sep=' ') # convert to np array
for i in range(0, len(b)): #chage number values in b
if (b[i] > 3):
b[i] = str(5 * b[i])
else:
b[i] = str(3 * b[i])
output.write('\t'.join(string_b) '\n')
When I ran the code I got an error:
Traceback (most recent call last):
File "C:\Users\Khant\Desktop\ex2.py", line 89, in <module>
output.write('\t'.join(b) '\n')
TypeError: sequence item 0: expected str instance, float found
Then I found that though I have b[i] = str(5 * b[i])
but the elements in b actually are still float numbers rather than string. So I have to change the code like:
//...same code as above...//
for i in range(0, len(b)): #chage number values in b
if (b[i] > 3):
b[i] = 5 * b[i]
else:
b[i] = 3 * b[i]
string_b = [str(int(elem)) for elem in b] # convert to string
output.write('\t'.join(string_b) '\n')
this time the code worked normally. So I just wonder why b[i] = str(5 * b[i])
cannot work like what I want?
CodePudding user response:
Your issue is that you're putting your strings back into your numpy array. An array in Numpy only accepts one kind of value, its dtype
. Other types get converted to that type if they're individually assigned (so e.g. assigning a float into an integer array looses the floating point details). If you print out b
after doing your loop, you'll see that each of the string values has been converted back to a float (if that hadn't worked, it would have raised an exception).
A better approach is probably to do all the mathematical stuff, then cast the array to contain strings afterwards (though an alternative that didn't use Numpy could also be quire reasonable). Here's what that might look like (replacing the inner loop):
mask = b > 3 # might as well use numpy stuff for the math, if we're using arrays
b[mask] *= 5
b[~mask] *= 3
string_b = b.astype(str)
output.write('\t'.join(string_b) '\n')
CodePudding user response:
You specifically told numpy to use
dtype=float
now if you access/write to an element of the array, numpy automatically tries to convert the input (in your case "str") back to a float.
See for better explanation:
import numpy as np
a=np.array([1.0,2.0,3.3])
a[0]="abc"
results in "ValueError: could not convert string to float: 'abc'"