How to save a mix of strings and numbers into text files in specified format using numpy.savetxt?
Date : March 29 2020, 07:55 AM
will be helpful for those in need You can't make an ndarray of nonhomogeneous array types, so stacking str_arr and flt_arr won't work. You could start by converting flt_arr into an array of strs doing something like this: >>> np.char.mod("%4.2f", flt_arr)
array(['10.01', '11.01', '12.02'],
dtype='|S5')
|
How to decode a numpy array of encoded literals/strings in Python3? AttributeError: 'numpy.ndarray' object has no attrib
Date : March 29 2020, 07:55 AM
seems to work fine In Python 3, I have the follow NumPy array of strings. , You have an array of bytestrings; dtype is S: In [338]: arr=np.array((b'first_element', b'element'))
In [339]: arr
Out[339]:
array([b'first_element', b'element'],
dtype='|S13')
In [340]: arr.astype('U13')
Out[340]:
array(['first_element', 'element'],
dtype='<U13')
In [341]: np.char.decode(arr)
Out[341]:
array(['first_element', 'element'],
dtype='<U13')
|
Write numpy array of numpy strings while encoding and removing all empty spaces (Python)
Date : March 29 2020, 07:55 AM
may help you . Starting with the array of strings from your previous question (and my answer): arr = np.array(['\tSTART\t 0\n', '12345 ABCDEFG', '1A 2B3C', '\nEN D'])
In [1153]: astr=''.join(arr)
In [1154]: astr
Out[1154]: '\tSTART\t 0\n12345 ABCDEFG1A 2B3C\nE N D'
In [1155]: import re
In [1156]: re.sub('\s','',astr)
Out[1156]: 'START012345ABCDEFG1A2B3CEND'
In [1157]: print(arr)
['\tSTART\t 0\n' '12345 ABCDEFG' '1A 2B3C' '\nE N D']
In [1158]: print(arr.tolist())
['\tSTART\t 0\n', '12345 ABCDEFG', '1A 2B3C', '\nE N D']
In [1164]: bstr=''.join(arr).encode('unicode_escape')
In [1165]: bstr
Out[1165]: b'\\tSTART\\t 0\\n12345 ABCDEFG1A 2B3C\\nE N D'
In [1166]: re.sub(b'\s',b'',bstr)
Out[1166]: b'\\tSTART\\t0\\n12345ABCDEFG1A2B3C\\nEND'
In [1168]: re.sub('\s','',astr).encode('unicode_escape')
Out[1168]: b'START012345ABCDEFG1A2B3CEND'
In [1177]: re.sub(b'\s',b'',astr.encode())
Out[1177]: b'START012345ABCDEFG1A2B3CEND'
In [1180]: b''.join(astr.encode().split())
Out[1180]: b'START012345ABCDEFG1A2B3CEND'
In [1181]: b''.join(astr.encode('unicode_escape').split())
Out[1181]: b'\\tSTART\\t0\\n12345ABCDEFG1A2B3C\\nEND'
In [1183]: (''.join(astr.split())).encode()
Out[1183]: b'START012345ABCDEFG1A2B3CEND'
|
numpy date issue - convert floating point year-week format to numpy's date format
Date : March 29 2020, 07:55 AM
wish helps you I have a floating point value representing the date with year-week format as follows: , Here is one way: In [43]: a = 201423.0000
In [48]: date = np.datetime64(str(int(a // 100)), 'Y') + np.timedelta64(int(a%100), 'W')
In [49]: date
Out[49]: numpy.datetime64('2014-06-05')
|
Compare numpy arrays whose entries are strings, and find locations of strings
Tag : python , By : Anthony Eden
Date : March 29 2020, 07:55 AM
hop of those help? With all entries from array2 present in array, we can use np.searchsorted - sidx = array2.argsort()
out = sidx[np.searchsorted(array2,array1.ravel(),sorter=sidx).reshape(array1.shape)]
out = np.searchsorted(array2,array1.ravel()).reshape(array1.shape)
|