[Tutor] simple search and replace...

Spencer Parker inthefridge at gmail.com
Wed Mar 31 21:30:56 CEST 2010


That was only a small example of a message...the message contains several
elements.  If I have the PID segment then I have OBR segment...an OBX, etc.
 So it would have to cycle through finding each element that is in the
message till the end.  I actually meant a list and not a
dictionary...terminology confusion in my head.

On Wed, Mar 31, 2010 at 11:58 AM, Alan Gauld <alan.gauld at btinternet.com>wrote:

>
> "Spencer Parker" <inthefridge at gmail.com> wrote
>
> MSH|~^\&|SCW|SMED|COR||20100305123742||ORU^R01|100305123742891|P|2.2|||||||PID|||001109660||TEST^Man||19920519|M||||||||||00055254593|699-63-1152|||||||||||
>>
>> What I want to do is where it says, PID, is add a line break right before
>> that to knock it down to a new line.
>>
>
> I may be missing something here but won't string.replace() do
> what you want?
>
> ie
>
> h7string =
> MSH|~^\&|SCW|SMED|COR||20100305123742||ORU^R01|100305123742891|P|2.2|||||||PID|||001109660||TEST^Man||19920519|M||||||||||00055254593|699-63-1152|||||||||||
>
> h7string = h7string.replace('PID', '\nPID')
>
>
>  I currently have a dictionary that contains all of the segments
>>
>
> Not sure what you use as the key but remember that dictionaries
> don't guarantee order so joining it all together again might be tricky.
>
>
>  wondering what the best way to approach this is.  I thought about a
>> regular
>> expression...but...I don't think that is the best way.
>>
>
> It might be but only if the replacement is much more complex than you
> suggest.
>
>
>  I was thinking of using a 'for i in segments' statement to run through the
>> dictionary of items.
>>
>
> If you need multiple changes you might need that but I suspect a simple
> replace() will do. Even for multiple places, if it's only to add a \n then
> use a re.sub() to do it all in one call.
>
> HTH,
>
>
> --
> Alan Gauld
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20100331/0d036450/attachment.html>


More information about the Tutor mailing list