2

I am writing code that uses numpy.fromstring to read arrays from xml element text.

It runs with no error, but what it reads is very strange.

for example

import numpy as np

nr = 24
r_string = '''
0.0000    0.0100    0.0200    0.0300    0.0400    0.0500    0.0600    0.0700
0.0800    0.0900    0.1000    0.1100    0.1200    0.1300    0.1400    0.1500
0.1600    0.1700    0.1800    0.1900    0.2000    0.2100    0.2200    0.2300
'''

r = np.fromstring(r_string, count = nr)

print(r)

prints the following(garbage)

[1.20737375e-153 1.48440234e-076 1.30354286e-076 6.96312257e-077
 6.01356142e-154 1.20737830e-153 1.82984908e-076 1.30354286e-076
 6.96312257e-077 3.22522589e-086 6.01347037e-154 6.03686893e-154
 1.39804459e-076 9.72377416e-072 3.24245662e-086 6.01347037e-154
 6.03686880e-154 1.39939399e-076 1.79371973e-052 1.91654811e-076
 8.54289848e-072 6.96312257e-077 6.01356142e-154 1.20738399e-153]

What is going on here?

I will appreciate help here.

2
  • 3
    By using the default sep, you trigger a deprecated use of the function. From the documentation: Passing sep='', the default, is deprecated since it will trigger the deprecated binary mode of this function. This mode interprets string as binary bytes, rather than ASCII text with decimal numbers. Commented Dec 17, 2019 at 12:46
  • See the docs: sep : str, optional The string separating numbers in the data; extra whitespace between elements is also ignored. Commented Dec 17, 2019 at 12:46

2 Answers 2

2

you need to declare sep=' '

>>> r = np.fromstring(r_string, count = nr, sep=' ')
>>> r
array([0.  , 0.01, 0.02, 0.03, 0.04, 0.05, 0.06, 0.07, 0.08, 0.09, 0.1 ,
       0.11, 0.12, 0.13, 0.14, 0.15, 0.16, 0.17, 0.18, 0.19, 0.2 , 0.21,
       0.22, 0.23])
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you, now it works. It is strange that 'sep' is defined as optional in the manual link
As other also explain the default, is deprecated since it will trigger the deprecated binary mode of this function - since version 1.14 - you can also see more about it in document link you have already mentaion above
@DeenaRoller Happy to help, and I see you're new to SO If this answer or any other one solved your issue, please mark it as accepted.
0

Running np.fromstring without the sep specified will actually throw the warning:

DeprecationWarning: The binary mode of fromstring is deprecated, as it behaves surprisingly on unicode inputs.

You need to specify your seperator, like:

np.fromstring(r_string, sep="\t")

Output:

array([0.  , 0.01, 0.02, 0.03, 0.04, 0.05, 0.06, 0.07, 0.08, 0.09, 0.1 ,
       0.11, 0.12, 0.13, 0.14, 0.15, 0.16, 0.17, 0.18, 0.19, 0.2 , 0.21,
       0.22, 0.23])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.