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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T11:30:44+00:00 2026-05-21T11:30:44+00:00

I need to read an ASCII file into Python, where an excerpt of the

  • 0

I need to read an ASCII file into Python, where an excerpt of the file looks like this:

E     M S T   N...
...
9998  1 1 128 10097 10098 10199 10198 20298 20299 20400 20399
9999  1 1 128 10098 10099 10200 10199 20299 20300 20401 20400
10000 1 1 128 10099 10100 10201 10200 20300 20301 20402 20401
10001 1 2  44  2071  2172 12373 12272
10002 1 2  44  2172  2273 12474 12373

The above should ideally be following NumPy schema:

array([(9998, 1, 1, 128, (10097, 10098, 10199, 10198, 20298, 20299, 20400, 20399)),
       (9999, 1, 1, 128, (10098, 10099, 10200, 10199, 20299, 20300, 20401, 20400)),
       (10000, 1, 1, 128, (10099, 10100, 10201, 10200, 20300, 20301, 20402, 20401)),
       (10001, 1, 2, 44, (2071, 2172, 12373, 12272)),
       (10002, 1, 2, 44, (2172, 2273, 12474, 12373))], 
      dtype=[('E', '<i4'), ('M', '<i4'), ('S', '<i4'), ('T', '<i4'), ('N', '|O4')])

Where the last object, N, is a tuple with between 2 and 8 integers.

I would like to load this ragged structure using either np.loadtxt or np.genfromtxt, except that I’m not sure if this is possible. Any built-in tips, or do I need to do a custom split-cast-for-loop?

  • 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-21T11:30:45+00:00Added an answer on May 21, 2026 at 11:30 am

    You do need a custom “split-cast” for loop, as far as I know.

    In fact, NumPy can read nested structures like yours, but they must have a fixed shape, like in

    numpy.loadtxt('data.txt', dtype=[ ('time', np.uint64), ('pos', [('x', np.float), ('y', np.float)]) ])
    

    When trying to read your data with the dtype that you need, NumPy only reads the first number of each tuple:

    dt=[('E', '<i4'), ('M', '<i4'), ('S', '<i4'), ('T', '<i4'), ('N', '|O4')]
    print numpy.loadtxt('data.txt', dtype=dt)
    

    thus prints

    [(9998, 1, 1, 128, '10097')
     (9999, 1, 1, 128, '10098')
     (10000, 1, 1, 128, '10099')…]
    

    So, I would say go ahead and use a for loop instead of numpy.loadtxt().

    You might also use an intermediate approach that might be faster: you let NumPy load the file with the above code, and then you manually “correct” the ‘N’ field:

    dt=[('E', '<i4'), ('M', '<i4'), ('S', '<i4'), ('T', '<i4'), ('N', '|O4')]
    arr = numpy.loadtxt('data.txt', dtype=dt)  # Correctly reads the first 4 columns
    
    with open('data.txt') as input_file:
        for (line_num, line) in enumerate(input_file):
            arr[line_num]['N'] = tuple(int(x) for x in line.split()[4:])  # Manual setting of the tuple column
    

    This approach might be faster than parsing the whole array in a for loop. This produces the result you want:

    [(9998, 1, 1, 128, (10097, 10098, 10199, 10198, 20298, 20299, 20400, 20399))
     (9999, 1, 1, 128, (10098, 10099, 10200, 10199, 20299, 20300, 20401, 20400))
     (10000, 1, 1, 128, (10099, 10100, 10201, 10200, 20300, 20301, 20402, 20401))
     (10001, 1, 2, 44, (2071, 2172, 12373, 12272))
     (10002, 1, 2, 44, (2172, 2273, 12474, 1237))]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to read test.TXT file(tab delimited) into MATLAB. TXT file have form: Datum
I have simple ascii text file like this: Madonna is a celebrity No she's
I would like to read a (fairly big) log file into a MATLAB string
I need to read 16 bits from the binary file as std::string or char
I need to read a file line by line, and change a variable accordingly.
I need to read data from XML to a List<>. The XML file contains
I need to read the size of a file but the server is forcing
I need to read and write some data on .mdb Access file and over
I need to read in an expression from a file using a string stream
I have a multi-page ascii file, and I need to be able to separate

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.