milka1117
milka1117

Reputation: 521

Removing different types of double quotes from string?

I have a list of names that has sizes in inches in it. Such as:

Asus VP248QG 24''

BenQ XYZ123456 32"

As you can see first name has double single-quote sign for inches while second name has normal double-quote sign.

I have this code to remove these sizes, because I do not need them:

def monitor_fix(s):
    if ('"' in s):
        return re.sub(r'\s+\d+(?:\.\d+)"\s*$', '', str(s))
    if ("''" in s):
        return re.sub(r"\s+\d+(?:\.\d+)''\s*$", '', str(s))

But it only removes ordinary double-quote sign, not the double single-quote sign. How to deal with this?

Upvotes: 2

Views: 110

Answers (2)

Guimoute
Guimoute

Reputation: 4649

Assuming the sizes are always well separated with spaces, we can simply remove the "word" that contains quotes. Bonus point because the size can be anywhere in the string too.

products = ["Asus VP248QG 24'' silver", 'BenQ XYZ123456 32"']

for n, product in enumerate(products):

    product_without_size = ""
    for word in product.split(" "):
        if not("''" in word or '"' in word):   # If the current word is not a size,
            product_without_size += word + " " # add it to the product name (else skip it).
    products[n] = product_without_size.rstrip(" ")

print(products) # ['Asus VP248QG silver', 'BenQ XYZ123456']

Using the format of your original post, it would look like this:

def monitor_fix(product):

    product_without_size = ""
    for word in product.split(" "):
        if not("''" in word or '"' in word):   # If the current word is not a size,
            product_without_size += word + " " # add it to the product name (else skip it).
    return product_without_size.rstrip(" ")

Upvotes: 1

Christoffer
Christoffer

Reputation: 31

You can simply remove the last 4 - 5 symbols with string[:]

list = ["Asus VP248QG 24''", 'BenQ XYZ123456 32"']

for i in range(len(list)):
    if "''" in list[i]:
        list[i] = list[i][:-5]
    if '"' in list[i]:
         list[i] = list[i][:-4]
    print(list[i])

Upvotes: 3

Related Questions