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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T20:15:20+00:00 2026-06-14T20:15:20+00:00

I am trying to import a simple tab separated text file using genfromtxt. I

  • 0

I am trying to import a simple tab separated text file using genfromtxt. I need to have access to each column header name, along with the data in the column associated with that name. Currently I am accomplishing this in a way that seems kind odd. All values in the txt file, including the header, are decimal numbers.

sample input file:

1     2     3     4      # header row
1.2   5.3   2.8   9.5
3.1   4.5   1.1   6.7
1.2   5.3   2.8   9.5
3.1   4.5   1.1   6.7
1.2   5.3   2.8   9.5
3.1   4.5   1.1   6.7


table_data = np.genfromtxt(file_path)       #import file as numpy array
header_values = table_data[0,:]             # grab first row
table_values = np.delete(table_data,0,0)    # grab everything else

I know there must be a more proper way to import a text file of data. I need to make it easy to access each column’s header and the respective data pertaining to that header value. I appreciate any help you can provide.

Clarification:

I want to be able to access a column of data by using something along the lines of table_values[header_of_first_column]. How would I accomplish this?

  • 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-14T20:15:21+00:00Added an answer on June 14, 2026 at 8:15 pm

    Use the names parameter to use the first valid line as column names:

    data = np.genfromtxt(
        fname,
        names = True, #  If `names` is True, the field names are read from the first valid line
        comments = '#', # Skip characters after #
        delimiter = '\t', # tab separated values
        dtype = None)  # guess the dtype of each column
    

    For example, if I modify the data you posted to be truly tab-separated, then the following code works:

    import numpy as np
    import os
    fname = os.path.expanduser('~/test/data')
    data = np.genfromtxt(
        fname,
        names = True, #  If `names` is True, the field names are read from the first valid line
        comments = '#', # Skip characters after #
        delimiter = '\t', # tab separated values
        dtype = None)  # guess the dtype of each column
    print(data)
    # [(1.2, 5.3, 2.8, 9.5) (3.1, 4.5, 1.1, 6.7) (1.2, 5.3, 2.8, 9.5)
    #  (3.1, 4.5, 1.1, 6.7) (1.2, 5.3, 2.8, 9.5) (3.1, 4.5, 1.1, 6.7)]
    
    print(data['1'])
    # [ 1.2  3.1  1.2  3.1  1.2  3.1]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

im trying to import html table data to an excel file and have succeeded
I am trying to import a large text file into a MySQL database. The
I have a txt file that I’m trying to import as flat file into
I'm trying to get this trivial parsec code to compile import Text.Parsec simple =
I'm trying to use the SQL Server Import Wizard to import flat text file
i am using python with gtk, trying to make a simple text editor with
I'm writing a simple import application and need to read a CSV file, show
I'm trying to define a very simple function in Drools as below: import java.util.List;
I am trying to connect a simple groovy script to the DB. Code: import
I'm trying to import an XML file that contains a list of points for

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.