【问题标题】:I want my code to print only if the result is different from previous result and not the same thing again我希望我的代码仅在结果与以前的结果不同且不再相同时打印
【发布时间】:2021-11-12 11:55:45
【问题描述】:

每当我运行代码时,此代码都会打印它从 api_url 获取的最新结果,但我希望它检查结果是否与上一个相同,然后不要打印它。

注意:这是我第一次在这里提问,如果我在问题中犯了任何错误,请原谅。

 api_url= "https://api.bscscan.com/api? module=account&action=tokentx&contractaddress=smartcontract&address=" + person1["address"] + \
    "&startblock=10290674&endblock=999999999&page=1&offset=1&sort=desc&apikey=APIKEY"

 response = requests.get(api_url)
 address_content = response.json()
 result = address_content.get("result")  

 for n, transaction in enumerate(result):
    block = transaction.get("blockNumber")
    hash = transaction.get("hash")
    tx_from = transaction.get("from")
    tx_to = transaction.get("to")
    value = transaction.get("value")
    confirmations = transaction.get("confirmations")

    coin_value = Decimal(value)/1000000000000000000

    coin_value = int(value)/1000000000
    print("COIN", coin_value)
    print("\n")
    
    if tx_to in (tx_sell, tx_sell2):
        print("sell")
    elif tx_from in (tx_sell, tx_sell2):
        print("buy")

【问题讨论】:

  • 以某种方式保留最后一个结果(pickle、文件、xml、...)在 scripstart 上加载它,将其与您当前的值进行比较。关于在这里存储信息的大量问题 - 搜索 python + pickle。如果您这样做,打印或不打印不再是问题。

标签: python api logging blockchain solidity


【解决方案1】:

将结果存储在类似文件中:

myfile = open(“result.txt”,’w’)

myfile.write(str(result))

【讨论】:

    猜你喜欢
    • 2021-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多