Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3623276
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T23:21:48+00:00 2026-05-18T23:21:48+00:00

I am reading in a file using numpy.genfromtxt which brings in columns of both

  • 0

I am reading in a file using numpy.genfromtxt which brings in columns of both strings and numeric values. One thing I need to do is detect the length of the input. This is all fine provided there are more than one value read into each array.

But…if there is only one element in the resulting array, the logic fails. I can recreate an example here:

import numpy as np
a = np.array(2.3)

len(a) returns an error saying:

TypeError: len() of unsized object

however, If a has 2 or more elements, len() behaves as one would expect.

import numpy as np
a = np.array([2.3,3.6])

len(a) returns 2

My concern here is, if I use some strange exception handling, I can’t distinguish between a being empty and a having length = 1.

EDIT:
@noskio suggested setting a = np.array([2.3]). The problem is, the actual genesis of a is by using numpy.genfromtxt. The code looks like this:

import numpy as np
indata = np.genfromtxt(some_filename, names=True,dtype=None)
a = indata['one_col_headername']

As a result, if indata is only one row in the file, a is a 0-d array.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T23:21:49+00:00Added an answer on May 18, 2026 at 11:21 pm

    If you need a one-liner (assuming the answer you are expecting is 1):

    In [1]: import numpy as np
    
    In [2]: a = np.array(2.3)
    
    In [3]: len(np.atleast_1d(a))
    Out[3]: 1
    

    This page explains why it was decided to implement 0-dimensional arrays in numpy.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am reading one file using ruby in which I have written regexp in
I have .zip file which contain csv data. I am reading .zip file using
I am reading an xml file using javascript and then I need to submit
I am reading a file using perl script. This file consists of strings with
I am reading one Persian text file (using PHP) with the help of below
I am reading binary data from one file that specifies intensity values across x
Reading file using java and jcifs on windows. I need to determine size of
I am reading one file using Ruby and I am able to filter out
I'm reading a file using bufferedreader, so lets say i have line = br.readLine();
I'm reading a csv file using c# here is a little code snippet. using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.