pythontypeerrorimportdata

TypeError: 'type' object is not subscriptable during reading data


I'm pretty new importing data and I´m trying to make def that reads in the presented order. The error is in the fuction, not in the data.

def read_eos(filename: str, order: dict[str, int] = {"density": 0, "pressure": 1, 
    "energy_density": 2}):
  
    # Paths to files
    current_dir = os.getcwd()
    INPUT_PATH = os.path.join(current_dir, "input")

    in_eos = np.loadtxt(os.path.join(INPUT_PATH, filename))
    eos = np.zeros(in_eos.shape)

    # Density
    eos[:, 0] = in_eos[:, order["density"]]
    eos[:, 1] = in_eos[:, order["pressure"]]
    eos[:, 2] = in_eos[:, order["energy_density"]]

    return eos

Solution

  • Looks like the problem is right in the type hint of one of your function parameters: dict[str, int]. As far as Python is concerned, [str, int] is a subscript of the type dict, but dict can't accept that subscript, hence your error message.

    The fix is fairly simple. First, if you haven't done so already, add the following import statement above your function definition:

    from typing import Dict
    

    Then, change dict[str, int] to Dict[str, int].