如何将布尔值附加到文本文件?

问题描述 投票:0回答:1

我正在尝试将布尔值添加到文本文件,但出现此错误:

Traceback (most recent call last):
  File "/Users/valentinwestermann/Documents/La dieta mediterranea_dhooks.py", line 32, in <module>
    f.write(variant["available"])
TypeError: write() argument must be str, not bool 

有人知道如何解决此问题吗? :)它应该用作补货监控器,并在机器人启动时制作产品可用性的文本版本,然后不断进行比较并通知用户有关产品补货的信息。

import bs4 as bs
import urllib.request
import discord
from discord.ext import commands
from dhooks import Webhook
import requests
import json

r = requests.get("https://www.feature.com/products.json")
products = json.loads((r.text))["products"]

for product in products:
    print("============================================")
    print(product["title"])
    print(product["tags"])
    print(product["published_at"])
    print(product["created_at"])
    print(product["product_type"])
    for variant in product["variants"]:
        print(variant['title'])
        print(variant['available'],"\n")

        data =("available")
with open("stock_index.txt","w") as f:
    for product in products:        
        for variant in product["variants"]:
            if variant['available'] == True:
                f.write(product["title"])
                f.write(variant['title'])
                print(variant["available"])
                f.write("--------------------")
python boolean text-files shopify monitoring
1个回答
0
投票
您可以先转换为字符串:

f.write(str(variant["available"]))

热门问题
推荐问题
最新问题