[Python] How to replace hashed value retrieved from API to human-readable in a CSV

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

  1. Stack

    Stack Membro Participativo

    I am retrieving a hashed value from an API and storing that value in a CSV. Is it possible to translate that value into its matching human-readable form before writing it to the CSV, perhaps using a dictionary? For example, if the value retrieved is "1a3f2940d", the CSV should store "Product 1", which is the accompanying value of the hash code. The syntax I attempted is below, but the output is not replacing the hashed value in the CSV that is generated.

    SKU_MAP = {
    'efccb6f7':'Product 1',
    '6050e30712fa':'Product 2',
    'f8a100f':'Product 3',
    }


    def export_to_csv(users):
    with open('/tmp/users.csv', mode='w', newline='') as file:
    writer = csv.writer(file)
    writer.writerow(['User Name', 'Assigned Products'])
    for user in users:
    # Convert skuId to readable names using the SKU_MAP dictionary
    product_list = [SKU_ID.get(license['skuId'], license['skuId']) for license in user['assignedLicenses']]
    writer.writerow([user['userPrincipalName'], ','.join(product_list)])

    Continue reading...

Compartilhe esta Página