【问题标题】:Filter a csv file (without panda) compilation error过滤一个 csv 文件(没有 panda)编译错误
【发布时间】:2022-01-04 05:01:00
【问题描述】:

晚上好,我有一个问题是我必须制作 2 个 python 文件,其中 TestFiltrado 过滤 .csv 文件的列并且恰好有 2 列),另一个称为:Testdefiltrado 打印这 2 列,但我明白了错误。

测试过滤器:

Almacenamiento_de_datos = namedtuple('DESAPARECIDOS_INDIA_2018_2020','Tipo_de_sangre,Nombres,Genero,Cuerpo,Distrito,'


#where are the columns CSV FILE
                                                                     'DENUNCIADO,Estatura,Edad,Cuando_Desparecio')


def Archivo_Csv(Archivo): 
    with open(Archivo, encoding = 'utf-8') as f:
        lineador = csv.reader(f, delimiter=";")
        next(lineador)
        lista =[(Almacenamiento_de_datos(Tipo_de_sangre, Nombres, Genero, Cuerpo, Distrito, DENUNCIADO, float(Estatura), int(Edad),
                                     Cuando_Desparecio)for Tipo_de_sangre, Nombres, Genero, Cuerpo, Distrito,DENUNCIADO,
                                                           Estatura, Edad, Cuando_Desparecio in lineador)]
        return lista
#open the csv file and access it to


 Storage which is namedtuple save it in the variable l i s t a


def Todos_Los_nombres (lista):
    for i in lista:
        Nom = next(i['Nombres'], i ['Edad'])
        return Nom

#en list iterate and find me the columns EN: h o m b r e s, (int) E d a d

这就是调用它的代码

def TestFiltrador (Desaparecidoscsv):
    Nom= Todos_Los_nombres(Desaparecidoscsv)
    print("Los nombres y sus edad son : ",Nom)
## Nom : iterates the first code above and prints

是这个错误

 Nom = next(i['Nombres'], i ['Edad'])
TypeError: 'generator' object is not subscriptable

我该怎么办? 我希望他们能帮助我,感谢看到我的问题

【问题讨论】:

标签: python csv


【解决方案1】:

您正在这里创建一个生成器表达式:

lista = [
    (
        Almacenamiento_de_datos(
            Tipo_de_sangre,
            Nombres,
            Genero,
            Cuerpo,
            Distrito,
            DENUNCIADO,
            float(Estatura),
            int(Edad),
            Cuando_Desparecio,
        )
        for Tipo_de_sangre, Nombres, Genero, Cuerpo, Distrito, DENUNCIADO, Estatura, Edad, Cuando_Desparecio in lineador
    )
]

如果您想要namedtuples 的列表,请执行以下操作:

lista = [
    Almacenamiento_de_datos(
        Tipo_de_sangre,
        Nombres,
        Genero,
        Cuerpo,
        Distrito,
        DENUNCIADO,
        float(Estatura),
        int(Edad),
        Cuando_Desparecio,
    )
    for Tipo_de_sangre, Nombres, Genero, Cuerpo, Distrito, DENUNCIADO, Estatura, Edad, Cuando_Desparecio in lineador
]

【讨论】:

  • 以这种方式组织它可以为我解决这个问题吗? ,感谢帮助
  • 它将消除“生成器”错误。不知道之后是否还会出现另一个错误。
猜你喜欢
  • 2018-01-29
  • 2017-07-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-27
  • 1970-01-01
  • 2018-06-26
相关资源
最近更新 更多