I have a string see attached file like that and i would like to modify it using a function
Giving section name parameter name and value to set
Also in case i want to modify several parameters for a same section how can i have position in file to avoid scanning x time for a same section :
&NAM_WRITE_COVER_TEX CLANG='FR'
/
&NAM_FRAC LECOCLIMAP = T
/
&NAM_PGD_GRID PGRID = 'CONF PROJ '
/
&NAM_CONF_PROJ_GRID
XLATCEN=43.,
XLONCEN=0.
NIMAX=12,
NJMAX=8,
XDX=60000.,
XDY=60000. /
&NAM_CONF_PROJ
XLAT0=43., XLON0=0., XRPK=1.0, XBETA=0. /
/
&NAM_PGD_SCHEMES CNATURE = 'ISBA' ,
CSEA = 'SEAFLX' ,
CTOWN = 'TEB' ,
CWATER = 'WATFLX' ,
LGARDEN = F
/
I could suggest using python regular expressions. http://docs.python.org/2/library/re.html . The regular expression will only use one pass.
def replace_value(matchobj):
new_repl_dict = {'XLATCEN': 54, 'XDX': 7000, 'CTOWN': "'NEW_VAL'"}
if matchobj.group(1) in new_repl_dict:
return matchobj.group(1) + matchobj.group(2) + new_repl_dict[matchobj.group(1)]
file = "&NAM_WRITE_COVER_TEX CLANG='FR'\n..."
re.sub('(XLATCEN|XDX|CTOWN)(\s*=\s*)([^\n]*)', replace_value, file)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.