First time here? Check out the Help page!
1 | initial version |
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,
2 | No.2 Revision |
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,
3 | No.3 Revision |
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+]': ""}
4 | No.4 Revision |
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+]': ""}
5 | No.5 Revision |
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+]': ""}
6 | No.6 Revision |
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+]': ""}
7 | No.7 Revision |
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+]': ""}