IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Python Discussion :

yaml.load_all: “TypeError: list indices must be integers, not str”


Sujet :

Python

  1. #1
    Membre du Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Juin 2014
    Messages
    54
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Alpes Maritimes (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Étudiant
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Juin 2014
    Messages : 54
    Points : 47
    Points
    47
    Par défaut yaml.load_all: “TypeError: list indices must be integers, not str”
    Bonjour les amis,

    J'ai le fichier yaml suivant que je veux parser en utilisant yaml.load_all:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
     
        Version: 1
        TxnCode: MPT
        MessageID: "FFH-18544-1388620740-795905"
        Recommendations: 
         - {ReqSegFlightRef: [[1,2]],TotalPriceRef: 1,PriceInfoRef: 1}
         - {ReqSegFlightRef: [[3,4,2]],TotalPriceRef: 2,PriceInfoRef: 2}
         - {ReqSegFlightRef: [[5,4,2]],TotalPriceRef: 3,PriceInfoRef: 3}
        Flights: 
         - {OpCarrier: SN,MktCarrier: SN,FlightNb: 2902,DptDate: "0109",DptTime:     "09:30",ArrTime: "11:05",DptAirport: VIE,ArrAirport: BRU}
         - {OpCarrier: SN,MktCarrier: SN,FlightNb: 243,DptDate: "0109",DptTime: "12:15",ArrTime: "17:00",DptAirport: BRU,ArrAirport: FNA}
         - {OpCarrier: OS,MktCarrier: LH,FlightNb: 6325,DptDate: "0109",DptTime: "06:30",ArrTime: "07:35",DptAirport: VIE,ArrAirport: MUC}
        Prices: 
         - {TotalPrice: 1574.14,BaseAmount: 1368.00,TotalTaxe: 206.14,TotalSurcharge: 0.00,TotalFee: 0.00}
         - {TotalPrice: 1633.57,BaseAmount: 1368.00,TotalTaxe: 265.57,TotalSurcharge: 0.00,TotalFee: 0.00}
         - {TotalPrice: 1636.57,BaseAmount: 1368.00,TotalTaxe: 268.57,TotalSurcharge: 0.00,TotalFee: 0.00}
      LFSDetails: |
        PriceInfos: 
         - {FaresRef: [1,2],Price: {Total: 1574.14},Amount: {Total: 1368.00},Taxes:  {Total: 206.14},Surcharges: {Total: 0.00},Fees: {Total: 0.00}}
         - {FaresRef: [3,4,2],Price: {Total: 1633.57},Amount: {Total: 1368.00},Taxes: {Total: 265.57},Surcharges: {Total: 0.00},Fees: {Total: 0.00}}
         - {FaresRef: [3,4,2],Price: {Total: 1636.57},Amount: {Total: 1368.00},Taxes: {Total: 268.57},Surcharges: {Total: 0.00},Fees: {Total: 0.00}}
        ListCabinRequested: []
        FareInfo: 
         - {RBD: M,FareBasis: BFFOWAT,PTC: ADT,BreakPointId: N,Availability: 9,Cabin: W,FareType: OB}
         - {RBD: B,FareBasis: BFFOWAT,PTC: ADT,BreakPointId: Y,Availability: 9,Cabin: W,FareType: OB}
         - {RBD: B,FareBasis: BFFOWAT,PTC: ADT,BreakPointId: N,Availability: 9,Cabin: M,FareType: OB}
        Currency: EUR
    J'ai le code suivant mais en essayant de l'exécuter j'ai eu l'erreur “TypeError: list indices must be integers, not str” et je n'ai pas compris comment faire pourl'éviter !


    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
     
    def yaml_load_all(iStream,iCodeFilter=None):
        for ayamldoc in yaml.load_all(iStream):
            lfsWrapper = LFSWrapper(iCodeFilter)
            lfsDetails = ayamldoc['LFSDetails'] #line 20
            if lfsDetails is not None:
                ayamldoc['LFSDetails'] = yaml.load(lfsDetails)
            lfsWrapper.loadDict(ayamldoc)
            if lfsWrapper.isValid():
                yield lfsWrapper
    Je pense que je suis entrain de rater un aspect fondamental concerant la structure des données et la facon dont yaml.load_all fonctionne!

    J'esperais avoir un peu d'aide ici! Auriez-vous des idées?

    Merci de votre aide!!

  2. #2
    Expert éminent

    Homme Profil pro
    Inscrit en
    Octobre 2008
    Messages
    4 300
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations forums :
    Inscription : Octobre 2008
    Messages : 4 300
    Points : 6 780
    Points
    6 780
    Par défaut
    Salut,

    Tu utilises ayamldoc['LFSDetails'] comme si c'était un dictionnaire contenant la clé 'LFSDetails' alors que python dit que c'est une liste.

    Fait un print ayamldoc pour voir ce que cela contient.

Discussions similaires

  1. Réponses: 10
    Dernier message: 07/05/2014, 15h05
  2. typeError:list indice must be integers,not dict
    Par Mozard Yao dans le forum Odoo (ex-OpenERP)
    Réponses: 2
    Dernier message: 09/01/2014, 23h49
  3. TypeError:indice must be integers
    Par Mozard Yao dans le forum Général Python
    Réponses: 2
    Dernier message: 08/01/2014, 20h13
  4. TypeError: list indices must be integers, not list
    Par dergeekpanda dans le forum Général Python
    Réponses: 3
    Dernier message: 26/11/2013, 18h38
  5. Réponses: 2
    Dernier message: 23/04/2009, 22h24

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo