简体   繁体   中英

The script is not saving my bad_responses correctly

I have brainstormed a lot of possibilities and created a bulk API call so I can import some products into my store. It works fine, products are imported correctly, however, I have trouble saving bad responses in a csv file.

Maybe I am doing something wrong or my indentation is not correct. Please, point me in a right direction or provide any advice for the future for not making similar mistakes.

This is the code:

df = pd.read_csv('edited_csv.csv')
bad_responses_list = []
for i in range(len(df)):
    endpoint = f"{base_url}/products"
    data = {
            "product_id": int(df['product_id'][i]),
            "title": df['title'][i],
            "discount_type": "percentage",
            "discount": 5, 
        }
        }
    response = requests.post(endpoint, json.dumps(data), headers=headers)
    status_code = response.status_code
    if status_code != 200 or status_code != 201:
        bad_responses_list.append([df['product_id'][i], response.status_code])

df_bad_responses = pd.DataFrame(bad_responses_list, columns=['product_id', 'status_code'])
df_bad_responses.to_csv('products_with_bad_responses.csv')

Now, when I run this it creates csv with good and bad responses, something like this:

product_id, status_code
7262783, 201
9458389, 201
0493788, 422
7273628, 422
7263728, 201

Thank you in advance!

the or in this line:

if status_code != 200 or status_code != 201:

needs to be an and

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM