未加星标

Python check if the item in the list is in the text file

字体大小 | |
[开发(python) 所属分类 开发(python) | 发布者 店小二05 | 时间 2018 | 作者 红领巾 ] 0人收藏点击收藏
output_file = open("processed.txt", "a") 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', "r") as list_file: for txid in receive_txids: if txid not in list_file: print "for ea txid " + txid print "NEW TRANSACTION"; output_file.write(txid + '\n')

that's my code

this is list.txt contents

db8cd79b4a0eafc6368bb65d2ff34d7d9c3d2016bee8528ab945a3d4bbad982d 2a93476e65b5500bc3d69856ddd512854d4939f1aabf488ac2806ec346a898a3 45b629a779e6e0bf6d160c37833a27f1f2cc1bfa34632d166cccae83e69eb6fe bf5b7bc1aeaf7fb43f5d39b549278bee6665872bd74274dd5fad80d043002a3e 1e5f49fa1d0df059b9d7da8452cde9fb5a312c823401f5ed4ed4eafb5f98c1b0 7dc7cd4afcebaf8f17575be8b9acf06adcaadfe7fa5528453246307aa36e6ea0 aefdb89b461c118529bec78b35fed46cc5d7050b39902552fa2408361284c746 ec6abb67828c79cbf0b74131f0acfddc509efc9743bed0811d2316007cdcc482

text.json looks something like this:

[ { "account" : "", "address" : "D8xWhR8LqSdSLTxRWwouQ3EiSnvcjLmdo6", "category" : "receive", "amount" : 1000.00000000, "confirmations" : 1963, "blockhash" : "4569322b4c8c98fba3ef4c7bda91b53b4ee82d268eae2ff7658bc0d3753c00ff", "blockindex" : 2, "blocktime" : 1394242415, "txid" : "45b629a779e6e0bf6d160c37833a27f1f2cc1bfa34632d166cccae83e69eb6fe", "time" : 1394242265, "timereceived" : 1394242265 }, { "account" : "", "address" : "D8xWhR8LqSdSLTxRWwouQ3EiSnvcjLmdo6", "category" : "receive", "amount" : 11.00000000, "confirmations" : 1194, "blockhash" : "eff3d32177bf19629fe0f8076807acbb02b34aedcbce1c27a19ce9872daecb7c", "blockindex" : 6, "blocktime" : 1394290663, "txid" : "bf5b7bc1aeaf7fb43f5d39b549278bee6665872bd74274dd5fad80d043002a3e", "time" : 1394290582, "timereceived" : 1394290582 }, { "account" : "", "address" : "DKLMkLZmiSVXtEavDpQ4dasjZvC178QoM9", "category" : "receive", "amount" : 1.00000000, "confirmations" : 1183, "blockhash" : "7b5d3ebeb994dbff0940504db9e407bd90cad8a5a1ace05dcba4bc508ca27aff", "blockindex" : 9, "blocktime" : 1394291510, "txid" : "1e5f49fa1d0df059b9d7da8452cde9fb5a312c823401f5ed4ed4eafb5f98c1b0", "time" : 1394291510, "timereceived" : 1394291578 }, { "account" : "", "address" : "DKLMkLZmiSVXtEavDpQ4dasjZvC178QoM9", "category" : "receive", "amount" : 1.00000000, "confirmations" : 1179, "blockhash" : "4d9bd6d2988bc749022c41d125f1134796aa314e0d0bde34eba855ad88e76a7f", "blockindex" : 21, "blocktime" : 1394291642, "txid" : "7dc7cd4afcebaf8f17575be8b9acf06adcaadfe7fa5528453246307aa36e6ea0", "time" : 1394291629, "timereceived" : 1394291629 }, { "account" : "", "address" : "DKLMkLZmiSVXtEavDpQ4dasjZvC178QoM9", "category" : "receive", "amount" : 1.00000000, "confirmations" : 1179, "blockhash" : "4d9bd6d2988bc749022c41d125f1134796aa314e0d0bde34eba855ad88e76a7f", "blockindex" : 20, "blocktime" : 1394291642, "txid" : "aefdb89b461c118529bec78b35fed46cc5d7050b39902552fa2408361284c746", "time" : 1394291637, "timereceived" : 1394291637 }, { "account" : "", "address" : "DKLMkLZmiSVXtEavDpQ4dasjZvC178QoM9", "category" : "receive", "amount" : 11.00000000, "confirmations" : 34, "blockhash" : "df34d9d44e87cd3315755d3e7794b10729fc3f5853c218ec237c43a89d918eb7", "blockindex" : 5, "blocktime" : 1394364125, "txid" : "ec6abb67828c79cbf0b74131f0acfddc509efc9743bed0811d2316007cdcc482", "time" : 1394348464, "timereceived" : 1394348464 } ]

I cannot for the life of me find out why this isn't working. It's printing "NEW TRANSACTION" every time it iterates through.

I want to check for each txid (transaction id) in json.txt, if it already exists in list.txt. If not, I want to write it to "processed.txt"

File objects don't support containment tests; you'd need to read your text file.

It'd be easiest for you to put all transaction ids in a set, then remove all txids found in your list.txt using set operations. Whatever is left are new transactions to write to the file:

with open('text.json') as json_file: recieve_txids = {o['txid'] for o in json.load(json_file) if o['category'] == 'recieve'} with open('list.txt', "r") as list_file: recieve_txids -= {l.strip() for l in list_file} with open('processed.txt', "w") as output_file: for txid in recieve_txids: output_file.write(txid + '\n')

If you needed access to the original JSON objects still, use a dictionary, with the txid as the key, then remove all elements from the dictionary found in the file:

with open('text.json') as json_file: recieve_txids = {o['txid']: o for o in json.load(json_file) if o['category'] == 'recieve'} with open('list.txt', "r") as list_file: for line in list_file: txid = l.strip() if txid in recieve_txids: del recieve_txids[txid]

本文开发(python)相关术语:python基础教程 python多线程 web开发工程师 软件开发工程师 软件开发流程

代码区博客精选文章
分页:12
转载请注明
本文标题:Python check if the item in the list is in the text file
本站链接:https://www.codesec.net/view/611039.html


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 开发(python) | 评论(0) | 阅读(83)