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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T06:01:14+00:00 2026-06-02T06:01:14+00:00

I am a bit familiar with Python. I have a file with information that

  • 0

I am a bit familiar with Python. I have a file with information that I need to read in a very specific way. Below is an example…

1
6
0.714285714286
0    0    1.00000000000
0    1    0.61356352337
...
-1  -1    0.00000000000
0    0    5.13787636499
0    1    0.97147643932
...
-1  -1    0.00000000000
0    0    5.13787636499
0    1    0.97147643932
...
-1  -1    0.00000000000
0 0 0 0   5.13787636499
0 0 0 1   0.97147643932
....

So every file will have this structure (tab delimited).

  • The first line must be read in as a variable as well as the second and third lines.
  • Next we have four blocks of code separated by a -1 -1 0.0000000000. Each block of code is ‘n’ lines long. The first two numbers represent the position/location that the 3rd number in the line is to be inserted in an array. Only the unique positions are listed (so, position 0 1 would be the same as 1 0 but that information would not be shown).
  • Note: The 4th block of code has a 4-index number.

What I need

  • The first 3 lines read in as unique variables
  • Each block of data read into an array using the first 2 (or 4 ) column of numbers as the array index and the 3rd column as the value being inserted into an array.
  • Only unique array elements shown. I need the mirrored position to be filled with the proper value as well (a 0 1 value should also appear in 1 0).
  • The last block would need to be inserted into a 4-dimensional 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-06-02T06:01:15+00:00Added an answer on June 2, 2026 at 6:01 am

    I rewrote the code. Now it’s almost what you need. You only need fine tuning.

    I decided to leave the old answer – perhaps it would be helpful too.
    Because the new is feature-rich enough, and sometimes may not be clear to understand.

    def the_function(filename):
        """
        returns tuple of list of independent values and list of sparsed arrays as dicts
        e.g. ( [1,2,0.5], [{(0.0):1,(0,1):2},...] )
        on fail prints the reason and returns None:
        e.g. 'failed on text.txt: invalid literal for int() with base 10: '0.0', line: 5'
        """
    
        # open file and read content
        try:
            with open(filename, "r") as f:
                data_txt = [line.split() for line in f]
        # no such file
        except IOError, e:
            print 'fail on open ' + str(e)
    
        # try to get the first 3 variables
        try:
            vars =[int(data_txt[0][0]), int(data_txt[1][0]), float(data_txt[2][0])]
        except ValueError,e:
            print 'failed on '+filename+': '+str(e)+', somewhere on lines 1-3'
            return
    
        # now get arrays
        arrays =[dict()]
        for lineidx, item in enumerate(data_txt[3:]):
            try:
                # for 2d array data
                if len(item) == 3:
                    i, j = map(int, item[:2])
                    val = float(item[-1])
                    # check for 'block separator'
                    if (i,j,val) == (-1,-1,0.0):
                        # make new array
                        arrays.append(dict())
                    else:
                        # update last, existing
                        arrays[-1][(i,j)] = val
                # almost the same for 4d array data
                if len(item) == 5:
                    i, j, k, m = map(int, item[:4])
                    val = float(item[-1])
                    arrays[-1][(i,j,k,m)] = val
            # if value is unparsable like '0.00' for int or 'text'
            except ValueError,e:
                print 'failed on '+filename+': '+str(e)+', line: '+str(lineidx+3)
                return
        return vars, arrays
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm relatively(read: stupid newbie) familiar with disassembly but this bit stumped me: I have
I've been programming in c++ for a bit now and Im very familiar with
Bit support question. Apologies for that. I have an application linked with GNU readline.
I am not too familiar with bit shifting so I have the following question.
My question is a bit abstract. We're all familiar with AJAX preloaders/spinners that come
We have applications on Solaris 10 servers that generate text logfiles. We need to
I have a simple bit of code that outputs the stream from a google
I am pretty familiar with CSS and have used quite a bit of javascript
I am not very familiar with command line Ubuntu (leave alone copying a file
Bit of a philosophical question. If you have a public website with a site

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.