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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T08:58:32+00:00 2026-05-29T08:58:32+00:00

I’m a Perl programmer who’s attempting to learn Python by taking some work I’ve

  • 0

I’m a Perl programmer who’s attempting to learn Python by taking some work I’ve done before and converting it over to Python. This is NOT a line-by-line translation. I want to learn the Python Technique to do this type of task.

I’m parsing a Windows INI file. Sections names are in the format:

[<type> <description>]

The <type> is a single word field and is not case sensitive. The <description> could be multiple words.

After a section, there are a bunch of parameters and values. These are in the form of:

 <parameter> = <value>

Parameters have no blank spaces and can only contain underscores, letters, and numbers (case insensitive). Thus, the first = is the divider between a parameter and the value. There might be white space separating the parameter and value around the equals sign. There might be extra white space at the beginning or end of the line.

In Perl, I used regular expressions for parsing:

while (my $line = <CONTROL_FILE>) {
    chomp($line);
    next if ($line =~ /^\s*[#;']/);     #Comments start with "#", ";", or "'"
    next if ($line =~ /^\s*$/);         #Ignore blank lines

    if ($line =~ /^\s*\[\s*(\w+)\s+(.*)/) {    #Section
        say "This is a '$1' section called '$2'";
    }
    elsif ($line =~ /^\s*(\w+)\s*=\s*(.*)/) {   #Parameter
       say "Parameter is '$1' with a value of '$2'";
    }
    else {      #Not Comment, Section, or Parameter
        say "Invalid line";
    }

}

The problem is that I’ve been corrupted by Perl, so I think the easiest way to do something is to use a regular expression. Here’s the code I have so far…

 for line in file_handle:
     line = line.strip

     # Comment lines and blank lines
     if line.find("#") == 1 \
             or line.find(";") == 1 \
             or line.whitespace:
         continue

    # Found a Section Heading
    if line.find("[") == 1:
        print "I want to use a regular expression here"
        print "to split the section up into two pieces"
    elif line.find("=") != -1:
        print "I want to use a regular expression here"
        print "to split the parameter into key and value"
    else
        print "Invalid Line"

There are several things that irritate me here:

  • There are two places where a regular expression just seem to be calling out to be used. What is the Python way of doing this splitting?
  • I make sure to strip white space on either side of the string, and rewrite the string. That way, I don’t have to do the stripping multiple times. However, I’m rewriting the string which I understand is a very inefficient operation in Python. What is the Python way to handle this issue?
  • In the end, my algorithm looks pretty much like my Perl algorithm, and that seems to say that I am letting my Perl thinking get in the way. How should my code be structured in Python?

I’ve been going through the various on line tutorials, and they’ve helped me with understanding the syntax, but not much in the way of handling the language itself — especially someone who tends to think in another language.

My question:

  • Should I use regular expressions? Or, is there another and better way to handle this?
  • Is my coding logic correct? How should I be thinking about parsing this file?
  • 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-29T08:58:33+00:00Added an answer on May 29, 2026 at 8:58 am

    While I don’t think this is your intention, the file format appears quite similar to Python’s built-in ConfigParser module. Sometimes the most “Pythonic” way is already provided for you. (:

    In more direct answer to your question: regular expressions may be a good way to do this. Otherwise, you could try the more basic (and less robust)

    (parameter, value) = line.split('=')
    

    This would throw an error if the line contained no or more than one ‘=’ character. You may want to test it first with '=' in line.

    Also:

    line.find("[") == 1
    

    is probably better replaced by

    line.startswith("[")
    

    Hope that helpls a little (:

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I have just tried to save a simple *.rtf file with some websites and
this is what i have right now Drawing an RSS feed into the php,
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
Does anyone know how can I replace this 2 symbol below from the string
I have this code to decode numeric html entities to the UTF8 equivalent character.

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.