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 8677381
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T20:26:33+00:00 2026-06-12T20:26:33+00:00

I am reading from text files with the code below: import numpy as np

  • 0

I am reading from text files with the code below:

import numpy as np
my_data = np.genfromtxt(resultsDirectory+'/Points.txt', delimiter=' ')
PointX = my_data[:,5]
PointY = my_data[:,11]

My input files are typically like this –

ParamA : 0 ParamB : 7 ParamC : 0 ParamD : 1 Result : FAIL Time : 0 Epsilon : 0.5
ParamA : 0 ParamB : 11 ParamC : 0 ParamD : 1 Result : FAIL Time : 2 Epsilon : 0.5
ParamA : 0 ParamB : 7 ParamC : 0 ParamD : 3 Result : FAIL Time : 2 Epsilon : 0.25
ParamA : 0 ParamB : 13 ParamC : 0 ParamD : 1 Result : FAIL Time : 7 Epsilon : 0.25
ParamA : 0 ParamB : 7 ParamC : 0 ParamD : 4 Result : FAIL Time : 8 Epsilon : 0.125
ParamA : 0 ParamB : 8 ParamC : 0 ParamD : 2 Result : FAIL Time : 1 Epsilon : 0.125
ParamA : 0 ParamB : 8 ParamC : 0 ParamD : 3 Result : FAIL Time : 3 Epsilon : 0.125
ParamA : 0 ParamB : 8 ParamC : 0 ParamD : 4 Result : FAIL Time : 6 Epsilon : 0.125
ParamA : 0 ParamB : 9 ParamC : 0 ParamD : 2 Result : FAIL Time : 6 Epsilon : 0.125
ParamA : 0 ParamB : 10 ParamC : 0 ParamD : 2 Result : FAIL Time : 5 Epsilon : 0.125
ParamA : 0 ParamB : 14 ParamC : 0 ParamD : 1 Result : FAIL Time : 6 Epsilon : 0.125

When I extract PointX from this I get

PointX = [7 11 7 13 7 8 8 8 9 10 14]
PointY = [1 1 3 1 4 2 3 4 2 2 1]

Now, sometimes my text files contain only a single line or are even empty.

For example, if the text file has only one line, the my_data array is like this –

[  nan   nan   0.    nan   nan   7.    nan   nan   0.    nan   nan   1.
   nan   nan   nan   nan   nan  0.    nan   nan   0.5]

In this case, my_data.shape returns (21,).

However reading the array PointX or PointY gives me an error as IndexError: invalid index. I wanted PointX=[7] and PointY=[1]. Or if the text file is empty, it should be PointX=[] and PointY=[].

How exactly should I solve this problem? Also I need my PointX and PointY to be an array in order not to break the code which is dependent on it.

Thank you.

  • 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-06-12T20:26:34+00:00Added an answer on June 12, 2026 at 8:26 pm

    Unfortunately, genfromtxt returns a 1D array if given a file with only one line, and returns a 2D array if given more than one line. You could handle the discrepancy by reshaping:

    import numpy as np
    my_data = np.genfromtxt('data', delimiter=' ')
    if my_data.ndim == 1:
        my_data = my_data.reshape(1, -1)
    PointX = my_data[:,5]
    PointY = my_data[:,11]
    print(PointX)
    print(PointY)
    

    yields

    [ 7.]
    [ 1.]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using the code below to read the the txt files from my
Below is part of my code to read data from a text file, strip
I have an old Fortran program for reading records from text files. The records
I seem to be encountering an error when reading from a text file. This
I am reading in from a text file using StreamReader into a string array
I have a large text file I am reading from and I need to
I'm having a problem where instead of reading a text file from the location
I am working on an assignment that deals with reading data from a text
I was reading the following text from Stanford's Programming Paradigms class , and I
How I can start reading from a specific byte? I have following code: try

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.