Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用pandas编辑.xml_Python_Xml_Pandas - Fatal编程技术网

Python 使用pandas编辑.xml

Python 使用pandas编辑.xml,python,xml,pandas,Python,Xml,Pandas,我有一个包含以下内容的XML文件: <CompanyData><Identifier>Country Context Afghanistan</Identifier> <LanguageCode>2057</LanguageCode><DataTypeId>CCO</DataTypeId><ISOAlpha3>AFG</ISOAlpha3> <DataSource>EDWH&l

我有一个包含以下内容的XML文件:

<CompanyData><Identifier>Country Context Afghanistan</Identifier>
<LanguageCode>2057</LanguageCode><DataTypeId>CCO</DataTypeId><ISOAlpha3>AFG</ISOAlpha3>
<DataSource>EDWH</DataSource><BuildDate>2019-09-17T18:53:59.973</BuildDate>
<DataSet><Name>Country context</Name><SourceName>Source</SourceName>
<SourceDate>2019-09-17T18:53:59.973</SourceDate><Data>
<Name>Total population (2019)</Name><Value>35,688,787</Value></Data><Data>
<Name>Birth cohort (2019)</Name><Value>1,083,460</Value></Data><Data>
<Name>Surviving Infants (surviving to 1 year per year, 2019)</Name><Value>1,151,687</Value></Data><Data>
<Name>Infant mortality rate (deaths &lt; 1 year per 1000 births, 2015)</Name><Value>66/1000</Value></Data><Data>
<Name>Child mortality rate (deaths &lt; 5 years per 1000 births, 2015)</Name><Value>91/1000</Value></Data><Data>
<Name>World Bank Index, IDA (2015)</Name><Value>2.69</Value></Data><Data>
<Name>Gross Nation Income (per capita US$, 2015)</Name><Value>610</Value></Data><Data>
<Name>No. of districts/territories (2018)</Name><Value>407</Value></Data></DataSet></CompanyData>
阿富汗的国家背景 2057CCOAFG EDWH2019-09-17T18:53:59.973 国家背景资源 2019-09-17T18:53:59.973 总人口(2019年)35688787 出生队列(2019年)1083460 存活婴儿(2019年存活至每年1岁)1151687 婴儿死亡率(2015年每千名婴儿一年死亡数)66/1000 儿童死亡率(2015年每1000名婴儿5年死亡数)91/1000 世界银行指数,国际开发协会(2015)2.69 国家总收入(2015年人均美元)610 地区/地区数量(2018)407 我需要更改这个.xml中的值(比如总人口)。 我正在考虑将这个.xml转换为.DF,执行更改并将其转换回.xml结构。但我没有找到任何解决方案将其转换为df并从df构建.xml。
可能还有另一种方法,比如直接编辑.xml?

我建议使用熊猫以外的其他方法。实际上,有一个xml库,您可能会发现它足以满足您的需要:

from xml.etree import ElementTree as et
tree = et.parse(xml_data)
tree.find('CompanyData.Identifier').text = "New Country Context"

查阅“x路径”了解更多关于选择它们的指南,但上述方法应该可以帮助您在不需要熊猫的情况下更改数据。

下面的代码将2个元素替换为虚拟值

你所需要做的就是用你需要的数据填充dict
new_data

from xml.etree import ElementTree as ET


xml = '''<?xml version="1.0" encoding="UTF-8"?>
<CompanyData>
   <Identifier>Country Context Afghanistan</Identifier>
   <LanguageCode>2057</LanguageCode>
   <DataTypeId>CCO</DataTypeId>
   <ISOAlpha3>AFG</ISOAlpha3>
   <DataSource>EDWH</DataSource>
   <BuildDate>2019-09-17T18:53:59.973</BuildDate>
   <DataSet>
      <Name>Country context</Name>
      <SourceName>Source</SourceName>
      <SourceDate>2019-09-17T18:53:59.973</SourceDate>
      <Data>
         <Name>Total population (2019)</Name>
         <Value>35,688,787</Value>
      </Data>
      <Data>
         <Name>Birth cohort (2019)</Name>
         <Value>1,083,460</Value>
      </Data>
      <Data>
         <Name>Surviving Infants (surviving to 1 year per year, 2019)</Name>
         <Value>1,151,687</Value>
      </Data>
      <Data>
         <Name>Infant mortality rate (deaths &lt; 1 year per 1000 births, 2015)</Name>
         <Value>66/1000</Value>
      </Data>
      <Data>
         <Name>Child mortality rate (deaths &lt; 5 years per 1000 births, 2015)</Name>
         <Value>91/1000</Value>
      </Data>
      <Data>
         <Name>World Bank Index, IDA (2015)</Name>
         <Value>2.69</Value>
      </Data>
      <Data>
         <Name>Gross Nation Income (per capita US$, 2015)</Name>
         <Value>610</Value>
      </Data>
      <Data>
         <Name>No. of districts/territories (2018)</Name>
         <Value>407</Value>
      </Data>
   </DataSet>
</CompanyData>'''

new_data = {'Total population (2019)': 10000,'World Bank Index, IDA (2015)': 7.45}
root = ET.fromstring(xml)
for field_name,new_value in new_data.items():
    value_element = root.find(".//Data/[Name='{}']".format(field_name))
    value_element.find('Value').text = str(new_value)
ET.dump(root)
从xml.etree导入ElementTree作为ET
xml=“”
阿富汗的国家背景
2057
CCO
AFG
EDWH
2019-09-17T18:53:59.973
国家背景
来源
2019-09-17T18:53:59.973
总人口(2019年)
35,688,787
出生队列(2019年)
1,083,460
存活婴儿(2019年存活至每年1岁)
1,151,687
婴儿死亡率(2015年每1000名婴儿1年死亡)
66/1000
儿童死亡率(2015年每1000名婴儿5年死亡)
91/1000
世界银行指数,国际开发协会(2015年)
2.69
国民总收入(2015年人均美元)
610
地区/地区数量(2018年)
407
'''
新的_数据={‘总人口(2019)’:10000,’世界银行指数,国际开发协会(2015)’:7.45}
root=ET.fromstring(xml)
对于字段\名称,在新\数据中添加新\值。项()
value_element=root.find(“.//Data/[Name='{}']”。格式(字段_Name))
值元素。查找('value')。text=str(新值)
ET.dump(根目录)
输出

<CompanyData>
   <Identifier>Country Context Afghanistan</Identifier>
   <LanguageCode>2057</LanguageCode>
   <DataTypeId>CCO</DataTypeId>
   <ISOAlpha3>AFG</ISOAlpha3>
   <DataSource>EDWH</DataSource>
   <BuildDate>2019-09-17T18:53:59.973</BuildDate>
   <DataSet>
      <Name>Country context</Name>
      <SourceName>Source</SourceName>
      <SourceDate>2019-09-17T18:53:59.973</SourceDate>
      <Data>
         <Name>Total population (2019)</Name>
         <Value>10000</Value>
      </Data>
      <Data>
         <Name>Birth cohort (2019)</Name>
         <Value>1,083,460</Value>
      </Data>
      <Data>
         <Name>Surviving Infants (surviving to 1 year per year, 2019)</Name>
         <Value>1,151,687</Value>
      </Data>
      <Data>
         <Name>Infant mortality rate (deaths &lt; 1 year per 1000 births, 2015)</Name>
         <Value>66/1000</Value>
      </Data>
      <Data>
         <Name>Child mortality rate (deaths &lt; 5 years per 1000 births, 2015)</Name>
         <Value>91/1000</Value>
      </Data>
      <Data>
         <Name>World Bank Index, IDA (2015)</Name>
         <Value>7.45</Value>
      </Data>
      <Data>
         <Name>Gross Nation Income (per capita US$, 2015)</Name>
         <Value>610</Value>
      </Data>
      <Data>
         <Name>No. of districts/territories (2018)</Name>
         <Value>407</Value>
      </Data>
   </DataSet>
</CompanyData>

阿富汗的国家背景
2057
CCO
AFG
EDWH
2019-09-17T18:53:59.973
国家背景
来源
2019-09-17T18:53:59.973
总人口(2019年)
10000
出生队列(2019年)
1,083,460
存活婴儿(2019年存活至每年1岁)
1,151,687
婴儿死亡率(2015年每1000名婴儿1年死亡)
66/1000
儿童死亡率(2015年每1000名婴儿5年死亡)
91/1000
世界银行指数,国际开发协会(2015年)
7.45
国民总收入(2015年人均美元)
610
地区/地区数量(2018年)
407