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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T20:40:59+00:00 2026-05-17T20:40:59+00:00

I have a file as follows: line 1: _____ … # for AAA #export

  • 0

I have a file as follows:

line 1: _____
   ...
# for AAA
#export CONFIG = AAA_defconfig

# for BBB, BbB, Bbb, BBb3
#export CONFIG = BBB_defconfig

# for CCC, CcC, Ccc, Ccc1
#export CONFIG = CCC_defconfig
   ...
other lines

I want manipulate the file, so that based on the given string, I could export the right “CONFIG”, and comment the others.
e.g. if I got “CcC”, then the file would be manipulated as

line 1: _____
    ...
# for AAA
#export CONFIG = AAA_defconfig

# for BBB, BbB, Bbb, BBb3
#export CONFIG = BBB_defconfig

# for CCC, CcC, Ccc, Ccc1
export CONFIG = CCC_defconfig
    ...
other lines

what the good way to do it in python?

Thanks in advance!!

  • 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-17T20:41:00+00:00Added an answer on May 17, 2026 at 8:41 pm

    Then why not make it as

    line = 'xxxx'
    CONFIG = default_deconfig
    
    if line == 'AAA':
        CONFIG =  AAA_defconfig
    elif line == 'CCC':
        CONFIG =  CCC_defconfig
    ...
    

    unless this is not a python file and you want to manipulate it. It looks like that.

    In that case create a config generator which will create a config file based on the line variable.

    [Edit: Based on comments]

    You might have to make some adjustments but this should work.

    # Simple , crude solution
    
    f = open('file1', 'r')
    manipulated_lines = []
    readFirstLine = False
    config = ''
    configComma = ''
    uncommentLine = 0
    for line in f:
        tokens = line.split()
    
        if uncommentLine == 1:
            # this is comment line
            if tokens[0] == '#export':
                manipulated_lines.append(line[1:])
                uncommentLine = uncommentLine + 1
                continue
        elif uncommentLine > 1:
            manipulated_lines.append(line)
            continue
    
        if not readFirstLine: 
            config = line.rstrip('\n')
            configComma = config + ','
            readFirstLine = True
    
        # Process additional lines 
        manipulated_lines.append(line)
    
        if len(tokens) > 0 and tokens[0] == '#':
            if tokens[1] == 'for':
                if config in tokens or configComma in tokens:
                    uncommentLine = uncommentLine + 1
                    continue
    
    print manipulated_lines
    f.close()
    fw = open('file2', 'w')
    fw.writelines(manipulated_lines)
    fw.close()
    

    Input: file1

    CCC
    # for AAA
    #export CONFIG = AAA_defconfig
    
    # for BBB, BbB, Bbb, BBb3
    #export CONFIG = BBB_defconfig
    
    # for CCC, CcC, Ccc, Ccc1
    #export CONFIG = CCC_defconfig
       ...
    

    Output: file2

    CCC
    # for AAA
    #export CONFIG = AAA_defconfig
    
    # for BBB, BbB, Bbb, BBb3
    #export CONFIG = BBB_defconfig
    
    # for CCC, CcC, Ccc, Ccc1
    export CONFIG = CCC_defconfig
       ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a flat file as follows: 11|aaa 11|bbb|NO|xxx 11|ccc 11|ddd|NO|yyy For lines that
I have a file of names and addresses as follows (example line) OSCAR ,CANNONS
I have an XML file as follows, and I'm trying to read the content
I have a ruby file as follows: module Example class Myclass def t_st Hello
I have a file with data listed as follows: 0, 2, 10 10, 8,
I have an aidl file defined as follows: package com.erbedo.callalert; interface RemoteCallAlert { void
In my .htaccess file I have a set of rules as follows: RewriteRule ^dir/page1$
Imagine you have an object foo that you saved as saved.file.rda as follows: foo
I have a html form for uploading a file, which is as follows: $uploadhtml
I have an XML document as follows: <directory> <file><monitored>0</monitored> <xferStatus>1</xferStatus> <name>test1.txt</name> <size>7</size> <created>03/31/10 11:30:02

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.