'Trying to access different parts of a tuple, getting: TypeError: list indices must be integers or slices, not str

I am trying to take a tuple that looks like the following:

('Sat Feb 29 13:32:59 +0000 2020', '1233746991752122368', 0.67, 0.293, 0.313, 0.316, 0.458, 'positive', 'joy')

And access the first part: 'Sat Feb 29 13:32:59 +0000 2020'

I then want to take specific parts of this tuple and assign them to different variables as seen in my code below, however I am running to the issue builtins.TypeError: list indices must be integers or slices, not str

    import calendar
def converting_timestamps(array):
    """reformat date column"""
    month_abbr = data[0][4:7]    
    month = list(calendar.month_abbr).index(month_abbr)
    year = data[0][26:30]
    date = data[0][8:11]
    time = data[0][11:20]
    return("{}-{}-{} {}".format(year, month, date, time))

What do I need to change within my code to fix this issue?

Entire relevant code:

import csv
import numpy as np
import pandas as pd
from math import log
from collections import Counter

import matplotlib.pyplot as plt
import matplotlib.colors as mcolors


def load_metrics(filename):
    """extract data based on filename"""
    lst = ['3','4','5','6','7','15']
    with open(filename, newline='') as File:
        rows=csv.reader(File,delimiter=",")
        reader = csv.reader(File)
        data1 = []
        for row in reader:
            data1.append(row)
    test = np.array(data1)
    first = test[:, 0:2]
    scnd = test[:, 7:14]
    third = test[:, 15:]
    data = np.concatenate((first,scnd,third),axis=1)
    return data
    pass


def unstructured_to_structured(data, indexes):
    """remove header row, convert values to float unless specified, then convert to tuple"""
    
    #convert to dataframe
    dataframe1 = pd.DataFrame(data, columns = ['0','1','2','3','4','5','6','7','8'])

    #code to remove header
    dropped = dataframe1.drop(0, axis=0)

    #code to convert all columns to float other than those specified by index
    update = dropped.astype({'2': 'f8','3': 'f8','4': 'f8','5': 'f8','6': 'f8'})

    #code to convert every row into a tuple
    data = update.to_records(index=False)
    return data
    pass

    import calendar
def converting_timestamps(array):
    """reformat date column"""
    month_abbr = data[0][4:7]    
    month = list(calendar.month_abbr).index(month_abbr)
    year = data[0][26:30]
    date = data[0][8:11]
    time = data[0][11:20]
    return("{}-{}-{} {}".format(year, month, date, time))


Solution 1:[1]

Try this,

    month_abbr = str(array[0][4:7]) # array[0] would be a string
    month_num = list(calendar.month_abbr).index(month_abbr)
    year = str(array[0][26:30])
    date = str(array[0][8:11])
    time = str(array[0][11:20])

Considering data as array, data() will act as a function to retrieve data using index, you need to use square brackets []

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1