[Tutor] strings and replace
Kent Johnson
kent37 at tds.net
Wed Sep 16 16:44:26 CEST 2009
On Wed, Sep 16, 2009 at 10:33 AM, Jojo Mwebaze <jojo.mwebaze at gmail.com> wrote:
> Hello There Again
>
> Might be a silly question but it has played me for the last two days!
>
> I have a string 'mystring' (pasted below), what i want to do is to change
> the values of of NAXIS1 (=1024) and NAXIS2 (=2048) to some other values!
> say NAXIS1 = 999 and NAXIS2 = 888.
>
> These old values that i want to change are not know forehand so i can not
> use
>
>>>> string.replace(1024, 999) #
>
> i also have tried
>
>>>> re.sub(r'(\d{4})', ' '+str(999), mystring)
>
> but this changes all occurrences of any 4 digit number in the string and
> also sets both NAXIS1 and NAXIS2 to the same value!
Just include NAXIS1/2 in the regexp. Something like
re.sub(r'NAXIS1 \(=\d{4}\)', 'NAXIS1 (=999)', mystring)
Kent
More information about the Tutor
mailing list