Python基于lis中的另一项获取一项的结果

2024-05-15 11:04:51 发布

您现在位置:Python中文网/ 问答频道 /正文

 [       
 {
    "account" : "",
    "address" : "D8xWhR8LqSdSLTxRWwouQ3EiSnvcjLmdo6",
    "category" : "send",
    "amount" : -1000.00000000,
    "fee" : -0.00000001,
    "confirmations" : 716,
    "blockhash" : "4569322b4c8c98fba3ef4c7bda91b53b4ee82d268eae2ff7658bc0d3753c00ff",
    "blockindex" : 2,
    "blocktime" : 1394242415,
    "txid" : "45b629a779e6e0bf6d160c37833a27f1f2cc1bfa34632d166cccae83e69eb6fe",
    "time" : 1394242259,
    "timereceived" : 1394242259
    },
    {
    "account" : "",
    "address" : "DCPFe1fs7qScDFvoTexYqo95LmnJJkjmu7",
    "category" : "receive",
    "amount" : 0.13370000,
    "confirmations" : 717,
    "blockhash" : "e9024e177b42ca23fed741fb90c39836de5f9c722a93157e50df2e3f2c318d77",
    "blockindex" : 26,
    "blocktime" : 1394242412,
    "txid" : "ce41b4c35b09ae582436b8138d62375840c32bd9ea0360457bd9f589012d2da3",
    "time" : 1394242315,
    "timereceived" : 1394242315
    },
    {
    "account" : "",
    "address" : "DCPFe1fs7qScDFvoTexYqo95LmnJJkjmu7",
    "category" : "receive",
    "amount" : 0.00100000,
    "confirmations" : 692,
    "blockhash" : "17eb2ef40b8bcb2ceb3d7f07d6545f03fc9bf41c8d28f759becd84a31e65e123",
    "blockindex" : 14,
    "blocktime" : 1394243788,
    "txid" : "2b099fd0ce6239c5c3c69e2ba70669c3069858908e42b8ca970bf213e555d715",
    "time" : 1394243669,
    "timereceived" : 1394243669
    },
    {
    "account" : "",
    "address" : "DCPFe1fs7qScDFvoTexYqo95LmnJJkjmu7",
    "category" : "send",
    "amount" : -0.00100000,
    "fee" : -2.00000000,
    "confirmations" : 692,
    "blockhash" : "17eb2ef40b8bcb2ceb3d7f07d6545f03fc9bf41c8d28f759becd84a31e65e123",
    "blockindex" : 14,
    "blocktime" : 1394243788,
    "txid" : "2b099fd0ce6239c5c3c69e2ba70669c3069858908e42b8ca970bf213e555d715",
    "time" : 1394243669,
    "timereceived" : 1394243669
    }
    ]

那是我的数据。你知道吗

我怎样才能看到“txid”中的条目,其中“category”=“receive”我还希望逐行写入一个名为列表.txt““

我对“category”=“send”的位置一点也不感兴趣

谢谢:-)

编辑: 这是我的密码

with (open('text.json') as f:
    data = json.load(f)
my_list = json.load(open("text.json"))
result = sum(item["category"] == "receive" for item in my_list)

i = 0
res = ""
while i < result:
    res = data[i]['txid']
    if data[i]['category'] == "receive":
        with open ("list.txt", "a") as myfile:
            myfile.write(res + "\n")
i += 1

Tags: sendjsontimeaddressaccountamountreceivecategory
1条回答
网友
1楼 · 发布于 2024-05-15 11:04:51
import json
object_list = json.load(open('text.json'))

receive_txids = [(x['txid'] + '\n') for x in object_list if x['category'] == 'receive']

output_file = open("list.txt", "a")
output_file.writelines(receive_txids)

编辑,这里有一个较长但更清晰(未经测试)的版本:

import json

with open('text.json') as json_file:
    object_list = json.load(json_file)

receive_txids = []
for object in object_list:
    if object['category'] == 'receive':
        receive_txids.append(object['txid'])

with open("list.txt", "a") as output_file:
    for txid in receive_txids:
        output_file.write(txid + '\n')

编辑:为什么我要建立一个列表,然后却什么都不做? 没有中间列表,简练的版本:

import json
object_list = json.load(open('text.json'))

output_file = open("list.txt", "a")
output_file.writelines([(x['txid'] + '\n') for x in object_list if x['category'] == 'receive'])

更整洁、更清晰的版本:

import json

with open('text.json') as json_file:
    object_list = json.load(json_file)

with open("list.txt", "a") as output_file:
    for object in object_list:
        if object['category'] == 'receive':
            output_file.writeline(object['txid'] + '\n')

相关问题 更多 >