我使用的,而不是一个Python字典我自己的文件时,但是当我在该文件上应用for
环路我收到此错误:蟒蛇for循环使用的文件,而不是字典
TypeError: string indices must be integers, not str
我的代码在下面给出其中“sai.json”是包含字典的文件。
import json
from naiveBayesClassifier import tokenizer
from naiveBayesClassifier.trainer import Trainer
from naiveBayesClassifier.classifier import Classifier
nTrainer = Trainer(tokenizer)
ofile = open("sai.json","r")
dataset=ofile.read()
print dataset
for n in dataset:
nTrainer.train(n['text'], n['category'])
nClassifier = Classifier(nTrainer.data, tokenizer)
unknownInstance = "Even if I eat too much, is not it possible to lose some weight"
classification = nClassifier.classify(unknownInstance)
print classification
'N'是一个字符串,而不是一本字典。请做一些关于如何解析json的研究。使用'json'模块 – Pynchia