Wednesday, 24 August 2022

Off-topic: numpy loading text file with strings and floats

 When using np.loadtxt to load a text file (first column a single letter, second a float), I found the best way was this:

cons = np.dtype([('res','U1'),('con','f')])
dt = np.loadtxt("/content/drive/MyDrive/Colab Notebooks/bonds/zip14a_cons.txt",dtype=cons)

Trying to use 'S', or 'S1' put a b in front of the text, with quote marks. Just using 'U' returned nothing.


No comments:

Post a Comment

Please enter your message here...