TypeError:“ int”对象不可下标-尝试创建csv文件时

时间:2018-09-18 15:56:08

标签: python csv dictionary int typeerror

我的字典auto_anno如下:

defaultdict(<class 'dict'>,
            {'Beda': {'Fuery': {'anger': 2,
                                'anticipation': 1,
                                'disgust': 2,
                                'fear': 2,
                                'sadness': 2}},
             'Fuery': {'Beda': {'surprise': 1},
                       'Fuery': {'anger': 1,
                                 'anticipation': 6,
                                 'disgust': 2,
                                 'fear': 1,
                                 'joy': 5,
                                 'sadness': 2,
                                 'surprise': 4,
                                 'trust': 4},
                       'Hawkeye': {'anger': 1, 'fear': 3, 'trust': 1},...#etc

我的目标是使用这些字典自动创建两个csv文件。一个用于节点的csv文件(字符的ID从0到x,以及它们的Label,即字符的名称),另一个用于节点的csv文件根据情感及其权重关系(此处:第一个dict的键是{{ 1}},而嵌套字典的键是source

到目前为止,我想出了一个使用target来加载上面的字典的函数:

pickle

但是我收到此错误消息:

def automate_gephi():
    """CREATES TWO CSV FILES TO USE IN GEPHI"""
    auto_anno = pickle.load(open("auto_anno.p", "rb"))
    characters = set()
    for char1,value in auto_anno.items(): # this is the predicted graph (a dictionary where each key is an experiencer and each value is a stimulus with emotions)
        for char2,val in value.items():
            characters.add(char1)
            characters.add(char2)

    file_node = open("nodes.csv", "w") #only nodes and id's go here
    file_node.write("Id"+"\t"+"Label"+"\n")

    # for each node create a numeric id and write to file
    for n,name in enumerate(characters):
        file_node.write(str(n)+"\t"+"%s" %name+"\n")
    file_node.close()

    # edges
    read_nodes = open("nodes.csv","r")
    edges_file = open("edges.csv","w")
    sep = "\t"
    edges_file.write("Source"+sep+"Target"+sep+"Label"+sep+"Weight"+"\n")
    Adjacency = {}

    for line in read_nodes:
        try:
            Adjacency[line.strip().split("\t")[1]] = line.strip().split("\t")[0]
        except IndexError:
            pass
        continue

    for key,value in auto_anno.items():
        source = key
        for k1,v1 in value.items():
            target = k1
            for emotion,weight in v1.items():
                try:
                    edges_file.write(str(Adjacency[source])+sep+str\
                                     (Adjacency[target])+sep+emotion+sep+\
                                     " ".join([i for i in weight["Weight"]])+"\n")
                except KeyError:
                    pass
    edges_file.close()

所需输出的示例:

文件1:节点

line 224, in automate_gephi
    " ".join([i for i in weight["Weight"]])+"\n")

TypeError: 'int' object is not subscriptable

文件2:边

Id  Label
0   Beda
1   Fuery
2   Hawkeye

我在这里想念什么?任何帮助表示赞赏!

谢谢!

1 个答案:

答案 0 :(得分:1)

在这一行

2633: 1801_13,
    293954: 293954_1,
    ean:    5035236232070,
    Nature: 301             
    creation_date: 2018-08-28T12:25:06.638Z,
    update_date: 2018-08-28T12:32:55.640Z,

您将for emotion,weight in v1.items() 设置为键,并将emotion设置为值。稍后您尝试使用此weight,由于weight["Weight"]在这一点上只是个数字,因此会给您带来错误。您需要修改列表理解能力,我/我们可以提供帮助,但是我不确定您到底打算如何使用它。