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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T21:44:30+00:00 2026-06-08T21:44:30+00:00

I am having difficulty in formatting some code in Python: My code is here:

  • 0

I am having difficulty in formatting some code in Python:
My code is here:

keys = ['(Lag)=(\d+\.?\d*)','\t','(Autocorrelation Index): (\d+\.?\d*)',       '(Autocorrelation Index): (\d+\.?\d*)',     '(Semivariance): (\d+\.?\d*)']

import re
string1 = ''.join(open("dummy.txt").readlines())
found = []
for key in keys:
found.extend(re.findall(key, string1))
for result in found:
    print '%s  =  %s' % (result[0],result[1])
raw_input()

So far, I am getting this output:

Lag = 1

Lag = 2

Lag = 3

Autocorrelation Index = #value

……

……

Semivariance = #value

But the desired output I want is:

 Lag        AutoCorrelation Index   AutoCorrelation Index   Semivariance
  1              #value                   #value               #value
  2              #value                   #value               #value
  3              #value                   #value               #value

If this output can be possible in a CSV file or a txt file, that would be great!

I think this is a way how you should output the loops, but I am not that great with loops.

My updated code (OLD version)

based on @mutzmatron answer

keys = ['(Lag)=(\d+\.?\d*)',
    '(Autocorrelation Index): (\d+\.?\d*)',
    '(Semivariance): (\d+\.?\d*)']

import re
string1 = open("dummy.txt").readlines().join()
found = []
for key in keys:
    found.extend(re.findall(key, string1))
raw_input()
for result in found:
    print '%s  =  %s' % (result[0], result[1])

raw_input()

not yet compiling! I am using IDLE python 2.6 , don’t know the error messages since I don’t know the pause command in the prompt!

Original Question

I am totally new to python and have a question. I am trying to process a large text file.
Here is just a snippet of it:

Band: WDRVI20((0.2*b4-b3)/((0.2*b4)+b3))
Basic Statistics:
  Min: -0.963805
  Max: 0.658219
  Mean: 0.094306
  Standard Deviation: 0.131797
Spatial Statistics, ***Lag=1***:
  Total Number of Observations (Pixels): 769995
  Number of Neighboring Pairs: 1538146
  Moran's I:
    ***Autocorrelation Index: 0.8482564597***
    Expected Value, if band is uncorrelated: -0.000001
    Standard Deviation of Expected Value (Normalized): 0.000806
    Standard Deviation of Expected Value (Randomized): 0.000806
    Z Significance Test (Normalized): 1052.029088
    Z Significance Test (Randomized): 1052.034915
  Geary's C:
    ***Autocorrelation Index: 0.1517324729***
    Expected Value, if band is uncorrelated: 1.000000
    Standard Deviation of Expected Value (Normalized): 0.000807
    Standard Deviation of Expected Value (Randomized): 0.000809
    Z Significance Test (Normalized): 1051.414163
    Z Significance Test (Randomized): 1048.752451
  ***Semivariance: 0.0026356529***
Spatial Statistics, Lag=2:
  Total Number of Observations (Pixels): 769995
  Number of Neighboring Pairs: 3068924
  Moran's I:
 Autocorrelation Index: 0.6230691635
   Expected Value, if band is uncorrelated: -0.000001
   Standard Deviation of Expected Value (Normalized): 0.000571
   Standard Deviation of Expected Value (Randomized): 0.000571
 Z Significance Test (Normalized): 1091.521976
 Z Significance Test (Randomized): 1091.528022
  Geary's C:
Autocorrelation Index: 0.3769372504
  Expected Value, if band is uncorrelated: 1.000000
  Standard Deviation of Expected Value (Normalized): 0.000574
  Standard Deviation of Expected Value (Randomized): 0.000587
 Z Significance Test (Normalized): 1085.700399
 Z Significance Test (Randomized): 1061.931158
Semivariance: 0.0065475488

I need to extract the information in between the star *** values ( eg : Autocorrelation Index, Semivariance values ) and process it , maybe write it to a different text file or excel file. Can I do that? Help would be really appreciated.

  • 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-08T21:44:32+00:00Added an answer on June 8, 2026 at 9:44 pm

    In order to format the data by section perhaps it’s easiest to work on the segments as follows

    keys =['(Lag)=(\d+\.?\d*)',
        '(Autocorrelation Index): (\d+\.?\d*)',
        '(Semivariance): (\d+\.?\d*)']
    
    import re
    string1 = ''.join(open("dummy.txt").readlines())
    
    sections = string1.split('Spatial Statistics')
    
    output = []
    heads = []
    
    for isec, sec in enumerate(sections):
        found = []
        output.append([])
        for key in keys:
            found.extend(re.findall(key, sec))
        for result in found:
            print '%s  =  %s' % (result[0],result[1])
            output[-1].append(result[1])
        if len(found) > 0 & len(heads) == 0:
            heads = [result[0] for result in found]    
    
    fout = open('output.csv', 'w')
    wrt = csv.writer(fout)
    wrt.writerow(heads)
    wrt.writerows(outputs)
    fout.close()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having difficulty converting some NSOperation code to ARC. My operation object uses a
I'm having difficulty getting my textarea to expand vertically automatically. I have some code
I'm having a difficulty formatting a table in html through python. I'm using for-loops
I've been having difficulty getting anything more than a simple index / to return
I am having difficulty with the following code which is inside a static method
I'm building a discussion board in Rails 3, but I'm having difficulty formatting the
Having difficulty checking (checkbox) all nodes by a specific class here is what i
I'm having trouble formatting a datetime.timedelta object. Here's what I'm trying to do: I
I'm having difficulty testing some logic that uses notifications. I've read about enforcing that
I'm having difficulty combining some lines in a comma delimited text file if the

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.