Question-and-Answer Resource for the Building Energy Modeling Community
Get started with the Help page
Ask Your Question

Revision history [back]

This should do what you want. It's based on something I used for preparing IDFs for jEPlus.

import fileinput

def edit_idf(substitutions, idf_path):
    """
    Replace items in an IDF in place (this changes the file directly so be careful).

    Parameters
    ----------
    substitutions: dict
        A dictionary mapping the variable to be substituted to the value
        to be substitute in its place, e.g. {'Block*:': ''} 
        will replace 'Block:' with '' in the input file.
    idf_path : str
        The path to the IDF or IMF.

    """
    for key in substitutions:
        for line in fileinput.FileInput(idf_path, inplace=True):
            for s in substitutions:
                 line = line.replace("%s" % s, str(substitutions[s]))  # enforce str
            print line,

This should do what you want. It's based on something I used for preparing IDFs for jEPlus.

import fileinput

def edit_idf(substitutions, idf_path):
    """
    Replace items in an IDF in place (this changes the file directly so be careful).

    Parameters
    ----------
    substitutions: dict
        A dictionary mapping the variable to be substituted to the value
        to be substitute in its place, e.g. {'Block*:': ''} 
        will replace 'Block:' 'Block*:' with '' in the input file.
    idf_path : str
        The path to the IDF or IMF.

    """
    for key in substitutions:
        for line in fileinput.FileInput(idf_path, inplace=True):
            for s in substitutions:
                 line = line.replace("%s" % s, str(substitutions[s]))  # enforce str
            print line,

This should do what you want. It's based on something I used for preparing IDFs for jEPlus.

import fileinput

def edit_idf(substitutions, idf_path):
    """
    Replace items in an IDF in place (this changes the file directly so be careful).

    Parameters
    ----------
    substitutions: dict
        A dictionary mapping the variable to be substituted to the value
        to be substitute in its place, e.g. {'Block*:': ''} 
        will replace 'Block*:' with '' in the input file.
file. You may also use regular
        expressions so {'Block[\d+]:': ''} would replace 'Block1:', 'Block2:',
        'Block999:', etc.              
    idf_path : str
        The path to the IDF or IMF.

    """
    for key in substitutions:
        for line in fileinput.FileInput(idf_path, inplace=True):
            for s in substitutions:
                 line = line.replace("%s" re.sub("%s" % s, str(substitutions[s]))  # enforce str
s), str(substitutions[s]), line)
            print line,

Your substitutions dict should be:

subs = {r'Block[\d+]': ""}

This should do what you want. It's based on something I used for preparing IDFs for jEPlus.

import fileinput
import re


def edit_idf(substitutions, idf_path):
    """
    Replace items in an IDF in place (this changes the file directly so be careful).

    Parameters
    ----------
    substitutions: dict
        A dictionary mapping the variable to be substituted to the value
        to be substitute in its place, e.g. {'Block*:': ''} 
        will replace 'Block*:' with '' in the input file. You may also use regular
        expressions so {'Block[\d+]:': ''} would replace 'Block1:', 'Block2:',
        'Block999:', etc.              
    idf_path : str
        The path to the IDF or IMF.

    """
    for key in substitutions:
        for line in fileinput.FileInput(idf_path, inplace=True):
            for s in substitutions:
                line = re.sub("%s" % s), str(substitutions[s]), line)
            print line,

Your substitutions dict should be:

subs = {r'Block[\d+]': ""}

This If you do want to use eppy (and I would recommend it!), @santoshphilip's answer is the way to go.

However, if you want an option that doesn't use eppy, this should do what you want. It's based on something I used for preparing IDFs for jEPlus.jEPlus.

import fileinput
import re


def edit_idf(substitutions, idf_path):
    """
    Replace items in an IDF in place (this changes the file directly so be careful).

    Parameters
    ----------
    substitutions: dict
        A dictionary mapping the variable to be substituted to the value
        to be substitute in its place, e.g. {'Block*:': ''} 
        will replace 'Block*:' with '' in the input file. You may also use regular
        expressions so {'Block[\d+]:': ''} would replace 'Block1:', 'Block2:',
        'Block999:', etc.              
    idf_path : str
        The path to the IDF or IMF.

    """
    for key in substitutions:
        for line in fileinput.FileInput(idf_path, inplace=True):
            for s in substitutions:
                line = re.sub("%s" % s), str(substitutions[s]), line)
line.rstrip())
            print line,

Your substitutions dict should be:

subs = {r'Block[\d+]': ""}

If you do want to use eppy (and I would recommend it!), @santoshphilip's answer is the way to go.

However, if you want an option that doesn't use eppy, this should do what you want. It's based on something I used for preparing IDFs for jEPlus.

import fileinput
import re


def edit_idf(substitutions, idf_path):
    """
    Replace items in an IDF in place (this changes the file directly so be careful).

    Parameters
    ----------
    substitutions: dict
        A dictionary mapping the variable to be substituted to the value
        to be substitute in its place, e.g. {'Block*:': ''} 
        will replace 'Block*:' with '' in the input file. You may also use regular
        expressions so {'Block[\d+]:': ''} would replace 'Block1:', 'Block2:',
        'Block999:', etc.              
    idf_path : str
        The path to the IDF or IMF.

    """
    for key in substitutions:
        for line in fileinput.FileInput(idf_path, inplace=True):
            for s in substitutions:
                line = re.sub("%s" % s), str(substitutions[s]), line.rstrip())
            print line,

Your substitutions dict should be:

subs = {r'Block[\d+]': ""}

If you want to use eppy (and I would recommend it!), @santoshphilip's answer is the way to go.

However, if you want an option that doesn't use eppy, for example if you want to use regular expressions in your search, this should do what you want. It's based on something I used for preparing IDFs for jEPlus.

import fileinput
import re


def edit_idf(substitutions, idf_path):
    """
    Replace items in an IDF in place (this changes the file directly so be careful).

    Parameters
    ----------
    substitutions: dict
        A dictionary mapping the variable to be substituted to the value
        to be substitute in its place, e.g. {'Block*:': ''} 
        will replace 'Block*:' with '' in the input file. You may also use regular
        expressions so {'Block[\d+]:': ''} would replace 'Block1:', 'Block2:',
        'Block999:', etc.              
    idf_path : str
        The path to the IDF or IMF.

    """
    for key in substitutions:
        for line in fileinput.FileInput(idf_path, inplace=True):
            for s in substitutions:
                line = re.sub("%s" % s), str(substitutions[s]), line.rstrip())
            print line,

Your substitutions dict should be:

subs = {r'Block[\d+]': ""}