[Tutor] strings and replace

Jojo Mwebaze jojo.mwebaze at gmail.com
Wed Sep 16 16:33:05 CEST 2009


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!

i also tried

>>> naxis = re.findall(r'(\d{4})',mystring[:360])
>>> mystring.replace(naxis[0], '  '+str(999))
>>> mystring.replace(naxis[1],'  '+str(888)) # the empty quotes are to keep
the len(mystring) the same before and after replacement

i am sure there must be a better ways to do it

Any ideas, i appreciate!

Cheers

Johnson




-----------------------------------------

mystring = "SIMPLE  =                    T / Fits format          BITPIX
=                  -32 / bits per pixel                 NAXIS
=                    2 / single image
NAXIS1  =                 1024 / x size
NAXIS2  =                 2048 / y
size                                         AIRMEND =                1.516
/ no comment                                     AIRMSTRT=
1.516 / no comment                                     DATE    =
'2005-01-15T12:32:00' / no comment
DATE-OBS= '2005-01-15T12:26:00' / no
comment                                    EXPTIME =                360.0 /
no comment                                     OBJECT  = 'herculesfield9
' / no comment                                     INSTRUME=
'MDM8K             ' / no comment
TELESCOP= 'MDM 2.4m          ' / no
comment                                     TEL_LAT =                31.95 /
no comment                                     TEL_LONG=
-111.6167 / no comment
TEL_ELEV=               1938.5 / no
comment                                     TEL_ZONE=                  7.0 /
no comment                                     CHIP_ID =
2 / no comment                                     FILT_ID =
'R                 ' / no comment
CTYPE1  = 'RA---TAN          ' / no
comment                                     CRVAL1  =        241.320458333 /
no comment                                     CRPIX1  =
-127.0 / no comment                                     CTYPE2  =
'DEC--TAN          ' / no comment
CRVAL2  =        16.7331388889 / no
comment                                     CRPIX2  =               1525.0 /
no comment                                     CD1_1   =
0.0 / no comment                                     CD1_2   =
-9.61111111111e-05 / no comment                                     CD2_1
=    9.61111111111e-05 / no comment
CD2_2   =                  0.0 / no
comment                                     STATMIN =          3179.140625 /
no comment                                     STATMAX =
3760.86035156 / no comment
STATMEAN=        3473.74743563 / no
comment                                     STATDEV =        56.3806567048 /
no comment                                     STATMED =
3469.74121094 / no comment                                     DATAMD5 =
'c8f7fd1759dcc82a42347e8e7eaf51db' / MD5 checksum                     END "
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20090916/55860feb/attachment.htm>


More information about the Tutor mailing list