[Python] Python Typeerror list indices must be integers or slices, not str

Discussão em 'Python' iniciado por Stack, Fevereiro 24, 2025.

  1. Stack

    Stack Membro Participativo

    I'm relatively new to Python and I'm trying to setup a script that will read a json file that I'm importing from an api and then write that file to a parquet file.

    In that JSON file I have fields with lists such as email addresses:

    {
    "cc_emails": [
    "Jimmy@somewhere.com",
    "Homer@Nutella.com"
    ],
    "fwd_emails": [],
    "reply_cc_emails": [
    "Jimmy@somewhere.com",
    "Homer@Nutella.com"
    ],
    "ticket_cc_emails": [
    "JimBeam@Bourbon.com",
    "Homer@Nutella.com"
    ],
    "ticket_bcc_emails": [],
    "fr_escalated": false,
    "spam": false,
    "email_config_id": 1234455,


    When I use the below code to try and turn these into a string ie:
    cc_emails Jimmy@Somewhere.com, Homer@Nutella.com

    I get the error list indices must be integers or slices, not str

    I've attempted to do this with this code:

    import requests
    import json
    import pandas as pd
    from pyspark.sql import SparkSession
    from datetime import datetime, timedelta
    from requests.auth import HTTPBasicAuth

    # Function to get tickets incrementally with pagination
    def get_incremental_tickets(updated_since):
    url = f'https://{FRESHDESK_DOMAIN}.freshdesk.com/api/v2/tickets'
    headers = {
    'Content-Type': 'application/json'
    }
    params = {
    'updated_since': updated_since,
    'per_page': 100 # Set the number of tickets per page to 100 (maximum allowed by Freshdesk)
    }
    all_tickets = []
    page = 1

    while True:
    params['page'] = page
    response = requests.get(url, headers=headers, params=params, auth=(API_KEY, 'X'))

    if response.status_code == 200:
    tickets = response.json()
    if not tickets:
    break
    all_tickets.extend(tickets)
    page += 1
    if page > 300:
    break
    else:
    print(f'Error: {response.status_code}, Response: {response.text}')
    break

    return all_tickets

    if __name__ == '__main__':
    # Fetch tickets updated since 2023 in smaller increments
    start_date = datetime(2025, 1, 1)
    all_tickets = []

    while start_date < datetime.now():
    end_date = start_date + timedelta(days=30)
    tickets = get_incremental_tickets(start_date.isoformat())
    if tickets:
    all_tickets.extend(tickets)
    print(f'Found {len(tickets)} tickets from {start_date} to {end_date}')
    start_date = end_date

    if all_tickets:
    print('Found Tickets')

    # Normalize the JSON data to include custom fields as columns
    df = pd.json_normalize(all_tickets, sep='_')

    # Flatten nested custom fields
    custom_fields_df = pd.json_normalize([ticket['custom_fields'] for ticket in all_tickets], sep='_')
    custom_fields_df['id'] = [ticket['id'] for ticket in all_tickets]
    df = df.merge(custom_fields_df, on='id', how='left')

    combined_cc_emails = [", ".join(sublist) for sublist in all_tickets["ticket_cc_emails"].tolist()]

    # Create a DataFrame with all fields
    df_combined = pd.DataFrame({'id': all_tickets['id'], 'combined_cc_emails': combined_cc_emails})


    df['responder_id'] = df['responder_id'].fillna(0).astype('int64')

    # Save DataFrame to a Parquet file
    parquet_table_name = f"{table_name}.parquet"
    parquet_file_path = parquet_file_path_base + parquet_table_name
    df.to_parquet(parquet_file_path, engine='pyarrow', index=False)
    print(f'Tickets saved to {parquet_file_path}')
    delta_table_name = f"{schema.lower()}.{table_name}"
    print(delta_table_name)

    # Load parquet file to data lake table
    spark = SparkSession.builder.appName("FabricNotebook").getOrCreate()
    df_spark = spark.read.parquet(parquet_file_path)

    # Write the Spark DataFrame to the Delta table
    df_spark.write.mode("overwrite").option("overwriteSchema", "true").format("delta").saveAsTable(delta_table_name)
    print(f'Tickets saved to Delta table {delta_table_name}')

    else:
    print('No tickets found or an error occurred.')



    Let me know if you need more code samples. Wasn't sure how much to include.

    My ultimate goal is to place these into a table. Hence the need to concatenate them together.

    error:

    ---------------------------------------------------------------------------
    TypeError Traceback (most recent call last)
    Cell In[23], line 69
    61 df = df.merge(custom_fields_df, on='id', how='left')
    63 # Ensure list fields are kept as arrays of strings
    64 # list_fields = ['cc_emails', 'fwd_emails', 'reply_cc_emails', 'ticket_cc_emails', 'ticket_bcc_emails', 'to_emails']
    65 # for field in list_fields:
    66 # df[field] = df[field].apply(lambda x: x if isinstance(x, list) else [])
    67 # Flatten the list of lists
    68 # Assuming all_tickets is a DataFrame
    ---> 69 combined_cc_emails = [", ".join(sublist) for sublist in all_tickets["ticket_cc_emails"].tolist()]
    71 # Create a DataFrame with all fields
    72 df_combined = pd.DataFrame({'id': all_tickets['id'], 'combined_cc_emails': combined_cc_emails})

    TypeError: list indices must be integers or slices, not str

    Continue reading...

Compartilhe esta Página