NEW_user 2020
NEW_user 2020

Reputation: 65

How to delete a long substring from a string without leaving empty lines?

I have a vcard string : I want to delete a line from this string , for example line related to TEL but when i use replace method , an empty line is left , how to delete the full line without leaving an empty line ?

vcard = """ BEGIN:VCARD
            VERSION:3.0
            UID:00000xcdfgedgrerfgrg
            N:LastName;FirstName;;;
            TEL;CELL:00000000 EMAIL:[email protected]
             END:VCARD
        """

Upvotes: 0

Views: 41

Answers (3)

John Gordon
John Gordon

Reputation: 33335

If you want to delete the entire line, then you have to replace the entire line, including leading spaces and the newline the end:

vcard = vcard.replace('            TEL;CELL:00000000 EMAIL:[email protected]\n', '')

Upvotes: 0

Chris
Chris

Reputation: 16147

'\n'.join([x for x in vcard.split() if 'TEL' not in x])

Upvotes: 0

Chris
Chris

Reputation: 80

Try this:

import re
vcard = """ BEGIN:VCARD
        VERSION:3.0
        UID:00000xcdfgedgrerfgrg
        N:LastName;FirstName;;;
        TEL;CELL:00000000 EMAIL:[email protected]
         END:VCARD
    """
re.sub(".*TEL.*\n?","",vcard)

Upvotes: 3

Related Questions