[Python] How do I shuffle randomly the output of csv,DictReader so that I am able to...

Discussão em 'Python' iniciado por Stack, Março 20, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to use ThreadPoolExecutor with 8 workers on a csv file data. When I read the csv file using csv.DictReader, I get a dictionaries with header as a key and each row having the value.

    Now when I run the ThreadPoolExecutor, all 8 workers start using the data in the first row which is not ideal. I want each worker(thread) to first import csv file, randomize the row data and then go through all of them using for loop.

    def grab_data(i):
    with open('devices.csv', 'r') as csv_file:
    csv_reader = csv.DictReader(csv_file)
    # Need something here to randomize the print
    print(*csv_reader)

    with ThreadPoolExecutor(max_workers=5, thread_name_prefix="API") as executor:
    executor.map(grab_data, range(1, 15))


    Current output will be same for all worker:

    {'A': 'FLR', 'B': 'E', 'C': 'SYM', 'D': 'DY', 'E': 'OE'}
    {'A': 'NZZ', 'B': 'QBS', 'C': 'CT', 'D': 'BI', 'E': 'JG'}
    {'A': 'MVU', 'B': 'RQ', 'C': 'IG', 'D': 'XD', 'E': 'AO'}
    {'A': 'XX', 'B': 'GWP', 'C': 'EA', 'D': 'FF', 'E': 'BW'}

    For Second Worker
    {'A': 'FLR', 'B': 'E', 'C': 'SYM', 'D': 'DY', 'E': 'OE'}
    {'A': 'NZZ', 'B': 'QBS', 'C': 'CT', 'D': 'BI', 'E': 'JG'}
    {'A': 'MVU', 'B': 'RQ', 'C': 'IG', 'D': 'XD', 'E': 'AO'}
    {'A': 'XX', 'B': 'GWP', 'C': 'EA', 'D': 'FF', 'E': 'BW'}


    I want for second worker to randomize the lines with output looking like this:

    {'A': 'MVU', 'B': 'RQ', 'C': 'IG', 'D': 'XD', 'E': 'AO'}
    {'A': 'FLR', 'B': 'E', 'C': 'SYM', 'D': 'DY', 'E': 'OE'}
    {'A': 'XX', 'B': 'GWP', 'C': 'EA', 'D': 'FF', 'E': 'BW'}
    {'A': 'NZZ', 'B': 'QBS', 'C': 'CT', 'D': 'BI', 'E': 'JG'}


    Now from here, I will use this data in a for loop:

    for line in csv_reader:
    r = requests.get(
    url=f"https://192.168.1.56:1813/rest/{line['A']}/sh/1/sl/{line['B']}/{line['C']}/status?",
    verify=False,
    auth=('random', 'TestTest'),
    )


    So that all workers are working on different data at a time, and not on same data.

    Continue reading...

Compartilhe esta Página