Skip to content

Commit

Permalink
added function to replace strings
Browse files Browse the repository at this point in the history
  • Loading branch information
Theodore Lindsay committed Apr 9, 2018
1 parent 0fd1571 commit 86ff386
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
3 changes: 2 additions & 1 deletion figurefirst/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
#reload(svg_to_axes)
from . import mpl_functions
from .svg_to_axes import FigureLayout
from . import mpl_fig_to_figurefirst_svg
from . import mpl_fig_to_figurefirst_svg
from . import svg_util
16 changes: 16 additions & 0 deletions figurefirst/svg_util.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#utility functions for dealing with svg files.

def replace_non_unique(in_filename,out_filename,
search_string = 'text_to_replace',
prefix = ''):
"""parse the document 'in_filename' and replace all
instances of the string 'search_string' with the
unique string 'prefix_x' where x is the occurance
of the search_string"""
with open(in_filename,'rt') as inf:
strdta = inf.read()
n_instances = strdta.count(search_string)
for i in range(n_instances):
strdta = strdta.replace(search_string, prefix + '_%s'%(i), 1)
with open(out_filename,'wt') as outf:
outf.write(strdta)

0 comments on commit 86ff386

Please sign in to comment.