Skip to content

fromstring/fromfile behave badly with malformed data (Trac #883) #1481

Closed

Description

@numpy-gitbot

Original ticket http://projects.scipy.org/numpy/ticket/883 on 2008-08-09 by @pv, assigned to unknown.

See the discussion at:

http://permalink.gmane.org/gmane.comp.python.numeric.general/24115

In summary:

  1. Empty string is understood as zero (except at the end of a string)

    np.fromstring('xxxxx', sep='x')
    array([ 0., 0., 0., 0., 0.])

    Missing values are treated as zeros. This is probably accidental, it's more likely that the memory allocated just happens to be filled with zeros...

  2. If count is given and not enough entries were read, the rest is padded with uninitialized data

    np.fromstring("23", dtype=np.float, sep="x", count=3)
    array([ 2.30000000e+001, 8.19397657e-269, 1.94625366e-312])

  3. Fromfile behaves differently from fromstring (Assume "foo.dat" contains string "23xxx")

    np.fromfile("foo.dat", dtype=np.float, sep="x")
    array([ 2.30000000e+001, 8.36783574e-269, 2.12885402e-313])
    np.fromfile("foo.dat", dtype=np.float, count=5, sep="x")
    5 items requested but only 3 read
    array([ 2.30000000e+001, 6.04468244e-269, -1.54891786e-039])

    Again, the last value is ignored, but the former ones are not treated as zero; instead, the array is filled with uninitialized data for the missing entries. Note also that neither warning or error is raised; instead, a message is printed to stdout.

Question: what is the correct behavior:

A) Raise ValueError on malformed data.

B) Raise ValueError on malformed data if count is given, otherwise truncate to valid part.

C) Truncate to valid part of data.

Activity

  1. numpy-gitbot commented on Oct 19, 2012

    @numpy-gitbot
    Author

    Milestone changed to 1.4.0 by @cournape on 2009-03-11

  2. numpy-gitbot commented on Oct 19, 2012

    @numpy-gitbot
    Author

    @charris wrote on 2009-05-24

    Note that the behavior i 3) has changed as of the current release (1.3.0). With the same file containing "23xxx"

    In [1]: fromfile("tmp", sep="x")
    Out[1]: array([ 23.,   0.,   0.,   0.])
    
    In [2]: fromfile("tmp", count=5, sep="x")
    5 items requested but only 4 read
    Out[2]: array([ 23.,   0.,   0.,   0.])
    

    Fromfile and fromstring behave differently, however. At the moment I'm guessing it comes from differences in fromstr_skip_separator and fromfile_skip_separator but I haven't looked closely at these functions yet.

  3. numpy-gitbot commented on Oct 19, 2012

    @numpy-gitbot
    Author

    Milestone changed to Unscheduled by @mwiebe on 2011-03-23

  4. charris commented on Feb 18, 2014

    @charris
    Member

    Still open in 1.9-devel.

  5. added a commit that references this issue on Mar 22, 2016
    ca6cfaf
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions