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

Asked 2 years ago, Updated 2 years ago, 24 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 4647
android x 1593
java x 1494
javascript x 1427
c x 927
c++ x 878
ruby-on-rails x 696
php x 692
python3 x 685
html x 656

© 2024 OneMinuteCode. All rights reserved.