[Python] I can't solve the problem of marking the excel sheet with specific words

Asked 1 weeks ago, Updated 1 weeks ago, 3 views

# Writing Code
from time import strftime
from openpyxl import Workbook

wb = Workbook()
ws = wb.create_sheet ("test")
wb.remove_sheet(wb['Sheet'])
ws.append (["Name", "Content", "Shipping", "Installation", "Price"])

contents = ["Delivery satisfaction", "Delivery installation is fast", "Delivery installation is kind", "Price is expensive", "Delivery installation price is all satisfied"]

for content in contents:
    product_name = "AAA"
    delivery = ""
    installation = ""
    price = ""
    mark = "O"

    If "shipping" in content:
        delivery = mark
        ws.append([product_name, content, delivery, installation, price])

    elif "install" in content:
        installation = mark
        ws.append([product_name, content, delivery, installation, price])

    elif "price" in content:
        price = mark
        ws.append([product_name, content, delivery, installation, price])

    else:
        ws.append([product_name, content, delivery, installation, price])

wb.save ("test.xlsx")
print ("Completed")


## the desired result
# Name Content Shipping Installation Price
# AAA Delivery Satisfaction O
# AAA Delivery Installation Quick OO
# AAA Installation Driver Kindness O
# AAA Price Expensive O
# AAA's delivery installation price is satisfactory


## Actual Test Results
# Name Content Shipping Installation Price
# AAA Delivery Satisfaction O
# AAA Delivery Installation Quick O
# AAA Installation Driver Kindness O
# AAA Price Expensive O
# AAA shipping installation price satisfactory O

python

2022-09-19 23:29

1 Answers

I think it's because you used the elif statement.
The elif statement is executed when the condition of the if statement or the elif statement above the phrase is false.
For example, the code below is

if "shipping" in content:
    delivery = mark
    ws.append([product_name, content, delivery, installation, price])

elif "install" in content:
    nstallation = mark
    ws.append([product_name, content, delivery, installation, price])

elif "price" in content:
    price = mark
    ws.append([product_name, content, delivery, installation, price])

else:
    ws.append([product_name, content, delivery, installation, price])

If content is "installation price

It ends with.

First and foremost, in all situations,

ws.append([product_name, content, delivery, installation, price])

a conditional sentence because of the code is used, out of the code is changed as follows.

if "shipping" in content:
    delivery = mark

elif "install" in content:
    nstallation = mark

elif "price" in content:
    price = mark

ws.append([product_name, content, delivery, installation, price])

And since you have to correspond to all the values of the content, if you change the elif statement to the if statement, it will change to the code below.

if "shipping" in content:
    delivery = mark

if "Install" in content:
    nstallation = mark

If "price" in content:
    price = mark

ws.append([product_name, content, delivery, installation, price])

If you do it like this, you will get the results you want.


2022-09-19 23:29

If you have any answers or tips

Popular Tags
python x 1174
android x 247
c x 235
java x 192
javascript x 106
mysql x 30
html x 27
node.js x 24
php x 21
list x 19

© 2022 pinfo. All rights reserved.