0

私のxmlファイルはこのようなものです

<S_Row>
     <S_Cell><S_CellBody></S_CellBody></S_Cell>
     <S_Cell><S_CellBody></S_CellBody></S_Cell>
     <S_Cell><S_CellBody></S_CellBody></S_Cell>
</S_Row>

私は次のようにPythonで処理しています:

for i, S_Cell in enumerate(S_Row.findall('S_Cell')):
        for S_CellBody in S_Cell.getchildren():
           if i==0:
              S_CellBody.text="ABC"

これにより、xml ファイルに次のような出力が得られます。

  <S_Row>
     <S_Cell><S_CellBody>ABC</S_CellBody></S_Cell>
     <S_Cell><S_CellBody></S_CellBody></S_Cell>
     <S_Cell><S_CellBody></S_CellBody></S_Cell>
   </S_Row>

別の行を追加して、次のように 2 行目 (1 列目) に要素を追加する場合:

   <S_Row>
     <S_Cell><S_CellBody>ABC</S_CellBody></S_Cell>
     <S_Cell><S_CellBody></S_CellBody></S_Cell>
     <S_Cell><S_CellBody></S_CellBody></S_Cell>
   </S_Row>

   <S_Row>
     <S_Cell><S_CellBody>DEF</S_CellBody></S_Cell>
     <S_Cell><S_CellBody></S_CellBody></S_Cell>
     <S_Cell><S_CellBody></S_CellBody></S_Cell>
   </S_Row>

私は何をすべきか?

4

1 に答える 1

0

を XMLstring と見なす

import cElementTree    
a1=cElementTree.fromstring(a)
strlst=['ABC','DEF','GHI']
for i,row in enumerate(a1.findall('S_Row')):    
    cb = row.getchildren()[0].getchildren()[0]
    cb.text = strlst[i+1]         
cElementTree.tostring(a1)
于 2013-04-23T09:09:35.243 に答える