簡體   English   中英

如何從python中的10000行一次讀取/處理100行?

[英]How to read/processes 100 lines at a time from 10000 lines in python?

如何一次通過/處理100行或以下的行try:

receipt_dict = {}
with open("data.txt", "r") as plain_text: // ** 10000+ lines **
    for line in plain_text:
        hash_value = line.strip()
        receipt_dict[hash_value] = 1



try:
    bitcoind.sendmany("", receipt_dict) // **here must loop 100 at a time**

將其作為字典list進行處理,並跟蹤每個字典的大小:

receipt_dicts = []
current_dict = {}
with open("data.txt", "r") as plain_text: # ** 10000+ lines **
    for line in plain_text:
        if len(current_dict) == 100:
            receipt_dict.append(current_dict)
            current_dict = {}
        current_dict[line.strip()] = 1
    receipt_dict.append(current_dict)

然后,您可以遍歷此list並一次處理一個字典。

帶發電機。 在這里, load_data_chunks數據累積到receipt_dict直到其大小超過chunk_size然后將其返回到下面的主循環中。

def load_data_chunks(path, fname, chunk_size): 
    receipt_dict = {}

    with open(fname, "r") as plain_text:
        for line in plain_text:
            hash_value = line.strip()
            receipt_dict[hash_value] = 1

            if len(receipt_dict) > chunk_size:
                yield receipt_dict
                receipt_dict = {}

    yield receipt_dict

for chunk in load_data_chunks("data.txt", 100):
    try:
        ...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM