I have no idea what this error is caused by, or how to fix it.
Basically, what I try to achieve is to read from a .csv file and make a dictionary from the information inside it. I've done it before without any problems, but this time it's really weird.
#Read External Data
DataNames = os.listdir("Data")
#Import Classes
ClassesPath = os.path.join("Data", DataNames[1])
Classes = open(ClassesPath)
global ClassesDict
ClassesDict = csv.DictReader(Classes, delimiter=",")
Upon trying to run
print(ClassesDict)
or
print(ClassesDict["ID"])
it always give me the error:
TypeError: 'DictReader' object is not subscriptable
I do know that lists, dictionaries, etc. are subscriptable objects, but my variable "ClassesDict" is (or should be) a dictionary.
Thank you very much in advance.
csv.DictReader
class provides an iterable interface over the csv data source where items are dictionaries:
reader = csv.DictReader(Classes, delimiter=",")
for row in reader:
print(row["ID"])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With