2016-03-01 27 views
0

我一直試圖弄清楚這幾個小時,但我似乎無法找到解決方案。我有以下xml格式:無法在python中替換xml中的值

<?xml version="1.0" encoding="utf-8"?><e3r> 
<moreData>false</moreData> 
<data> 
<CashDividend Cusip="" Sedol="" Isin="FR0007052782" TickerCode="" CommonCode="" ExchangeCode="" WPK="" actionID="" Declared_Date="" Ex_Date="" Record_Date="" Pay_Date="" Due_Bill_Redemption_Date="" Confirmation_Date="" Election_Date="" Adjustment_Date="" Net_Amount="" Gross_Amount="" Issuance_Fee="" Tax_Rate_PCT="" Supplemental_Amount="" Tax_Credit="" Foreign_Income="" Franking_PCT="" Percent_of_Par_PCT="" Info_Source="" Type="" Frequency="" Currency="" Amount_Status="" Sequence="" Depositary="" Adjustment_Factor="" Dividend_Note="" Change_Rate_Frequency="" TBA_Flag="" Abnormal_Ex_Date="" Mandatory_Optional="" Status="" Projected_Dividend_Amount="" Stock_Option_DRP="" Reinvestment_Ratio="" Reinvestment_Price="" DRP_Discount_PCT="" EUSD_TID="" EUSD_TID_SW="" Bonus_Amount="" CheckBoxForAdjFact="" AdjustmentFactor="" GrossAmount1="" GrossAmount2="" GrossAmount3="" GrossAmount4="" Currency1="" Currency2="" Currency3="" Currency4="" CashTermsOld="" RightsNew="" RightsOld="" SharesNew="" SharesOld="" SubPeriodFrom="" SubPeriodTo="" TradingFrom="" TradingTo="" RightsBBID="" RightsSECID="" TotalShrsMln="" CashCompId="" CashSecId="" CashTermsNew="" /> 
<CashDividend Cusip="" Sedol="" Isin="FR0007052782" TickerCode="" CommonCode="" ExchangeCode="" WPK="" actionID="" Declared_Date="" Ex_Date="" Record_Date="" Pay_Date="" Due_Bill_Redemption_Date="" Confirmation_Date="" Election_Date="" Adjustment_Date="" Net_Amount="" Gross_Amount="" Issuance_Fee="" Tax_Rate_PCT="" Supplemental_Amount="" Tax_Credit="" Foreign_Income="" Franking_PCT="" Percent_of_Par_PCT="" Info_Source="" Type="" Frequency="" Currency="" Amount_Status="" Sequence="" Depositary="" Adjustment_Factor="" Dividend_Note="" Change_Rate_Frequency="" TBA_Flag="" Abnormal_Ex_Date="" Mandatory_Optional="" Status="" Projected_Dividend_Amount="" Stock_Option_DRP="" Reinvestment_Ratio="" Reinvestment_Price="" DRP_Discount_PCT="" EUSD_TID="" EUSD_TID_SW="" Bonus_Amount="" CheckBoxForAdjFact="" AdjustmentFactor="" GrossAmount1="" GrossAmount2="" GrossAmount3="" GrossAmount4="" Currency1="" Currency2="" Currency3="" Currency4="" CashTermsOld="" RightsNew="" RightsOld="" SharesNew="" SharesOld="" SubPeriodFrom="" SubPeriodTo="" TradingFrom="" TradingTo="" RightsBBID="" RightsSECID="" TotalShrsMln="" CashCompId="" CashSecId="" CashTermsNew="" /> 
<CashDividend Cusip="" Sedol="" Isin="FR0007052782" TickerCode="" CommonCode="" ExchangeCode="" WPK="" actionID="" Declared_Date="" Ex_Date="" Record_Date="" Pay_Date="" Due_Bill_Redemption_Date="" Confirmation_Date="" Election_Date="" Adjustment_Date="" Net_Amount="" Gross_Amount="" Issuance_Fee="" Tax_Rate_PCT="" Supplemental_Amount="" Tax_Credit="" Foreign_Income="" Franking_PCT="" Percent_of_Par_PCT="" Info_Source="" Type="" Frequency="" Currency="" Amount_Status="" Sequence="" Depositary="" Adjustment_Factor="" Dividend_Note="" Change_Rate_Frequency="" TBA_Flag="" Abnormal_Ex_Date="" Mandatory_Optional="" Status="" Projected_Dividend_Amount="" Stock_Option_DRP="" Reinvestment_Ratio="" Reinvestment_Price="" DRP_Discount_PCT="" EUSD_TID="" EUSD_TID_SW="" Bonus_Amount="" CheckBoxForAdjFact="" AdjustmentFactor="" GrossAmount1="" GrossAmount2="" GrossAmount3="" GrossAmount4="" Currency1="" Currency2="" Currency3="" Currency4="" CashTermsOld="" RightsNew="" RightsOld="" SharesNew="" SharesOld="" SubPeriodFrom="" SubPeriodTo="" TradingFrom="" TradingTo="" RightsBBID="" RightsSECID="" TotalShrsMln="" CashCompId="" CashSecId="" CashTermsNew="" /> 
<CashDividend Cusip="" Sedol="" Isin="FR0007052782" TickerCode="" CommonCode="" ExchangeCode="" WPK="" actionID="" Declared_Date="" Ex_Date="" Record_Date="" Pay_Date="" Due_Bill_Redemption_Date="" Confirmation_Date="" Election_Date="" Adjustment_Date="" Net_Amount="" Gross_Amount="" Issuance_Fee="" Tax_Rate_PCT="" Supplemental_Amount="" Tax_Credit="" Foreign_Income="" Franking_PCT="" Percent_of_Par_PCT="" Info_Source="" Type="" Frequency="" Currency="" Amount_Status="" Sequence="" Depositary="" Adjustment_Factor="" Dividend_Note="" Change_Rate_Frequency="" TBA_Flag="" Abnormal_Ex_Date="" Mandatory_Optional="" Status="" Projected_Dividend_Amount="" Stock_Option_DRP="" Reinvestment_Ratio="" Reinvestment_Price="" DRP_Discount_PCT="" EUSD_TID="" EUSD_TID_SW="" Bonus_Amount="" CheckBoxForAdjFact="" AdjustmentFactor="" GrossAmount1="" GrossAmount2="" GrossAmount3="" GrossAmount4="" Currency1="" Currency2="" Currency3="" Currency4="" CashTermsOld="" RightsNew="" RightsOld="" SharesNew="" SharesOld="" SubPeriodFrom="" SubPeriodTo="" TradingFrom="" TradingTo="" RightsBBID="" RightsSECID="" TotalShrsMln="" CashCompId="" CashSecId="" CashTermsNew="" /> 
</data> 
<exs> 
<ex e="Information" l="Page: 'Distribution' Regex: '' " m="Page not found in source" i="" t=""> 
</ex> 
</exs> 
</e3r> 

我想要做的就是在「Cusip」字段中添加一些值。有人可以幫忙嗎?

我一直在使用下列內容:

import xml.etree.ElementTree as ET 
tree = ET.parse('//lo-dfs/users/username/Desktop/test.xml') 
root = tree.getroot() 

data = root.getchildren()[1] 
print data 

for child in data: 

    child.attrib['Cusip']= "Test" 

是否有人可以幫助我在做什麼錯在這裏?

謝謝

+0

你是否有錯誤?你有沒有把樹寫回磁盤(我假設你想存儲更改?) – mgilson

+0

我沒有得到任何錯誤,是的,我確實想存儲更改。你能推薦一種如何做到這一點的方法嗎? @mgilson – Nant

+0

你的xml貼出來似乎不正確 –

回答

0

找到利用一切CashDividend元素,例如findall(),設置屬性值,你已經這樣做,並通過tree.write()轉儲修改樹到XML文件:

import xml.etree.ElementTree as ET 

tree = ET.parse('test.xml') 
root = tree.getroot() 

for cash_dividend in root.findall("CashDividend"): 
    cash_dividend.attrib['Cusip']= "Test" 

tree.write('test.xml') 

Works的我。