pandas.DataFrame.to_xml#

DataFrame.to_xml(path_or_buffer=None, index=True, root_name='data', row_name='row', na_rep=None, attr_cols=None, elem_cols=None, namespaces=None, prefix=None, encoding='utf-8', xml_declaration=True, pretty_print=True, parser='lxml', stylesheet=None, compression='infer', storage_options=None)[源代码]#

将DataFrame呈现为XML文档。

1.3.0 新版功能.

参数
path_or_buffer字符串、路径对象、类似文件的对象或无,默认为无

字符串、路径对象(实现 os.PathLike[str] )或类似文件的对象实现 write() 功能。如果没有,则以字符串形式返回结果。

index布尔值,默认为True

是否在XML文档中包含索引。

root_name字符串,默认的‘data’

XML文档中根元素的名称。

row_name字符串,默认‘行’

XML文档中的行元素的名称。

na_rep字符串,可选

缺少数据表示形式。

attr_cols类似列表,可选

要作为属性写入行元素中的列的列表。分层列将被展平,并使用下划线分隔不同级别。

elem_cols类似列表,可选

要作为行元素中的子级写入的列的列表。默认情况下,所有列都作为行元素的子元素输出。分层列将被展平,并使用下划线分隔不同级别。

namespacesDICT,可选

要在根元素中定义的所有命名空间。DICT的键应该是DICT对应的URI的前缀名称和值。应为默认命名空间提供空字符串关键字。例如::

namespaces = {"": "https://example.com"}
prefix字符串,可选

要用于文档中每个元素和/或属性的命名空间前缀。这应该是其中的关键之一 namespaces 迪克特。

encoding字符串,默认‘utf-8’

对生成的文档进行编码。

xml_declaration布尔值,默认为True

是否在文档开头包含XML声明。

pretty_print布尔值,默认为True

输出是否应使用缩进和换行符进行美观打印。

parser{‘lxml’,‘etree’},默认‘lxml’

用于构建树的解析器模块。仅支持‘lxml’和‘etree’。通过‘lxml’,支持使用XSLT样式表的能力。

stylesheet字符串、路径对象或类似文件的对象,可选

URL、类似文件的对象或包含用于转换原始XML输出的XSLT脚本的原始字符串。脚本应使用原始输出中的元素和属性的布局。这一论点需要 lxml 待安装。目前仅支持XSLT 1.0脚本,不支持更高版本。

compression字符串或词典,默认为‘INFER’

For on-the-fly compression of the output data. If 'infer' and 'path_or_buffer' path-like, then detect compression from the following extensions: '.gz', '.bz2', '.zip', '.xz', or '.zst' (otherwise no compression). Set to None for no compression. Can also be a dict with key 'method' set to one of {'zip', 'gzip', 'bz2', 'zstd'} and other key-value pairs are forwarded to zipfile.ZipFile, gzip.GzipFile, bz2.BZ2File, or zstandard.ZstdDecompressor, respectively. As an example, the following could be passed for faster compression and to create a reproducible gzip archive: compression={'method': 'gzip', 'compresslevel': 1, 'mtime': 1}.

在 1.4.0 版更改: Z标准支持。

storage_optionsDICT,可选

对特定存储连接有意义的额外选项,例如主机、端口、用户名、密码等。对于HTTP(S)URL,键-值对被转发到 urllib.request.Request 作为标题选项。对于其他URL(例如,以“s3://”和“gcs://”开头),键-值对被转发到 fsspec.open 。请看 fsspecurllib 有关更多详细信息和有关存储选项的更多示例,请参阅 here

退货
无或字符串

如果 io 为None,则以字符串形式返回结果XML格式。否则返回None。

参见

to_json

将Pandas对象转换为JSON字符串。

to_html

将DataFrame转换为html。

示例

>>> df = pd.DataFrame({'shape': ['square', 'circle', 'triangle'],
...                    'degrees': [360, 360, 180],
...                    'sides': [4, np.nan, 3]})
>>> df.to_xml()  
<?xml version='1.0' encoding='utf-8'?>
<data>
  <row>
    <index>0</index>
    <shape>square</shape>
    <degrees>360</degrees>
    <sides>4.0</sides>
  </row>
  <row>
    <index>1</index>
    <shape>circle</shape>
    <degrees>360</degrees>
    <sides/>
  </row>
  <row>
    <index>2</index>
    <shape>triangle</shape>
    <degrees>180</degrees>
    <sides>3.0</sides>
  </row>
</data>
>>> df.to_xml(attr_cols=[
...           'index', 'shape', 'degrees', 'sides'
...           ])  
<?xml version='1.0' encoding='utf-8'?>
<data>
  <row index="0" shape="square" degrees="360" sides="4.0"/>
  <row index="1" shape="circle" degrees="360"/>
  <row index="2" shape="triangle" degrees="180" sides="3.0"/>
</data>
>>> df.to_xml(namespaces={"doc": "https://example.com"},
...           prefix="doc")  
<?xml version='1.0' encoding='utf-8'?>
<doc:data xmlns:doc="https://example.com">
  <doc:row>
    <doc:index>0</doc:index>
    <doc:shape>square</doc:shape>
    <doc:degrees>360</doc:degrees>
    <doc:sides>4.0</doc:sides>
  </doc:row>
  <doc:row>
    <doc:index>1</doc:index>
    <doc:shape>circle</doc:shape>
    <doc:degrees>360</doc:degrees>
    <doc:sides/>
  </doc:row>
  <doc:row>
    <doc:index>2</doc:index>
    <doc:shape>triangle</doc:shape>
    <doc:degrees>180</doc:degrees>
    <doc:sides>3.0</doc:sides>
  </doc:row>
</doc:data>