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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
U
¬ý°dúAã@sþdZddlmZddlZddlZddlmZmZddlm    Z    m
Z
m Z m Z m Z ddlmZddlmZddlmZdd    lmZdd
lmZdd lmZdd lmZmZer´dd lmZeededddGdd„dƒƒZGdd„deƒZ Gdd„deƒZ!dS)zF
:mod:`pandas.io.formats.xml` is a module for formatting data in XML.
é)Ú annotationsN)Ú TYPE_CHECKINGÚAny)ÚCompressionOptionsÚFilePathÚ
ReadBufferÚStorageOptionsÚ WriteBuffer©ÚAbstractMethodError)Údoc)Ú is_list_like)Úisna)Ú _shared_docs)Ú
get_handle)Úget_data_from_filepathÚpreprocess_data)Ú    DataFrameÚstorage_optionsÚcompression_optionsÚpath_or_buffer)rrc@söeZdZdZd8dd    d
d d d d d d d ddddddddœdd„Zddœdd„Zddœdd„Zddœdd„Zddœd d!„Zddœd"d#„Z    ddœd$d%„Z
d&dœd'd(„Z d)d*d*d+œd,d-„Z d.dd/œd0d1„Z d)d*dd+œd2d3„Zd)d*dd+œd4d5„Zd dœd6d7„ZdS)9ÚBaseXMLFormattera¼
    Subclass for formatting data in XML.
 
    Parameters
    ----------
    path_or_buffer : str or file-like
        This can be either a string of raw XML, a valid URL,
        file or file-like object.
 
    index : bool
        Whether to include index in xml document.
 
    row_name : str
        Name for root of xml document. Default is 'data'.
 
    root_name : str
        Name for row elements of xml document. Default is 'row'.
 
    na_rep : str
        Missing data representation.
 
    attrs_cols : list
        List of columns to write as attributes in row element.
 
    elem_cols : list
        List of columns to write as children in row element.
 
    namespaces : dict
        The namespaces to define in XML document as dicts with key
        being namespace and value the URI.
 
    prefix : str
        The prefix for each element in XML document including root.
 
    encoding : str
        Encoding of xml object or document.
 
    xml_declaration : bool
        Whether to include xml declaration at top line item in xml.
 
    pretty_print : bool
        Whether to write xml document with line breaks and indentation.
 
    stylesheet : str or file-like
        A URL, file, file-like object, or a raw string containing XSLT.
 
    {compression_options}
 
        .. versionchanged:: 1.4.0 Zstandard support.
 
    {storage_options}
 
    See also
    --------
    pandas.io.formats.xml.EtreeXMLFormatter
    pandas.io.formats.xml.LxmlXMLFormatter
 
    NTÚdataÚrowúutf-8Úinferrz7FilePath | WriteBuffer[bytes] | WriteBuffer[str] | NoneÚboolz
str | Nonezlist[str] | Nonezdict[str | None, str] | NoneÚstrz bool | Nonez5FilePath | ReadBuffer[str] | ReadBuffer[bytes] | NonerrÚNone)ÚframerÚindexÚ    root_nameÚrow_nameÚna_repÚ    attr_colsÚ    elem_colsÚ
namespacesÚprefixÚencodingÚxml_declarationÚ pretty_printÚ
stylesheetÚ compressionrÚreturncCsž||_||_||_||_||_||_||_||_|    |_|
|_    | |_
| |_ | |_ ||_ ||_||_|jj ¡|_| ¡|_| ¡| ¡| ¡|_| ¡dS©N)rrr r!r"r#r$r%r&r'r(r)r*r+r,rÚcolumnsÚtolistÚ    orig_colsÚprocess_dataframeÚ frame_dictsÚvalidate_columnsÚvalidate_encodingÚget_prefix_uriÚ
prefix_uriÚhandle_indexes)Úselfrrr r!r"r#r$r%r&r'r(r)r*r+r,r©r:úLd:\z\workplace\vscode\pyvenv\venv\Lib\site-packages\pandas/io/formats/xml.pyÚ__init__fs,
 
zBaseXMLFormatter.__init__Úbytes©r-cCs t|ƒ‚dS)ú—
        Build tree from  data.
 
        This method initializes the root and builds attributes and elements
        with optional namespaces.
        Nr
©r9r:r:r;Ú
build_tree’szBaseXMLFormatter.build_treecCsP|jr&t|jƒs&tt|jƒj›dƒ‚|jrLt|jƒsLtt|jƒj›dƒ‚dS)zä
        Validate elems_cols and attrs_cols.
 
        This method will check if columns is list-like.
 
        Raises
        ------
        ValueError
            * If value is not a list and less then length of nodes.
        z" is not a valid type for attr_colsz" is not a valid type for elem_colsN)r$r Ú    TypeErrorÚtypeÚ__name__r%r@r:r:r;r4›s ÿÿz!BaseXMLFormatter.validate_columnscCst |j¡dS)z×
        Validate encoding.
 
        This method will check if encoding is among listed under codecs.
 
        Raises
        ------
        LookupError
            * If encoding is not available in codecs.
        N)ÚcodecsÚlookupr(r@r:r:r;r5°s z"BaseXMLFormatter.validate_encodingzdict[int | str, dict[str, Any]]cCs6|j}|jr| ¡}|jdk    r*| |j¡}|jddS)zÊ
        Adjust Data Frame to fit xml output.
 
        This method will adjust underlying data frame for xml output,
        including optionally replacing missing values and including indexes.
        Nr )Zorient)rr Z reset_indexr#ZfillnaÚto_dict)r9Zdfr:r:r;r2¾s 
 z"BaseXMLFormatter.process_dataframecs\ˆjs
dSttˆjƒƒ}‡fdd„ˆj| ¡Dƒ}ˆjrF|ˆjˆ_ˆjrX|ˆjˆ_dS)zd
        Handle indexes.
 
        This method will add indexes into attr_cols or elem_cols.
        Ncsg|]}|ˆjkr|‘qSr:)r1)Ú.0Úxr@r:r;Ú
<listcomp>Ûs
z3BaseXMLFormatter.handle_indexes.<locals>.<listcomp>)r ÚnextÚiterr3Úkeysr$r%)r9Z    first_keyZindexesr:r@r;r8Ðs
 ÿ zBaseXMLFormatter.handle_indexescCs t|ƒ‚dS)zâ
        Get uri of namespace prefix.
 
        This method retrieves corresponding URI to prefix in namespaces.
 
        Raises
        ------
        KeyError
            *If prefix is not included in namespace dict.
        Nr
r@r:r:r;r6ås zBaseXMLFormatter.get_prefix_uriÚdictcCsLi}|jr(|jdkr(dd„|j ¡Dƒ}|jrH|jrHdd„|j ¡Dƒ}|S)zÃ
        Define other namespaces.
 
        This method will build dictionary of namespaces attributes
        for root element, conditionally with optional namespaces and
        prefix.
        NcSsi|]\}}|dkrd|“qS©ÚÚxmlnsr:©rHÚpÚnr:r:r;Ú
<dictcomp>þsz5BaseXMLFormatter.other_namespaces.<locals>.<dictcomp>cSsi|]\}}|dkrd|“qSrOr:rRr:r:r;rUs)r&r'Úitems)r9Z    nmsp_dictr:r:r;Úother_namespacesós      z!BaseXMLFormatter.other_namespacesúdict[str, Any]r©ÚdÚelem_rowr-c    Csj|js
|S|jD]T}| |¡}z"t||ƒs>t||ƒ|j|<Wqtk
rbtd|›ƒ‚YqXq|S)z´
        Create attributes of row.
 
        This method adds attributes using attr_cols to row element and
        works with tuples for multindex or hierarchical columns.
        úno valid column, )r$Ú_get_flat_col_namerrÚattribÚKeyError)r9rZr[ÚcolÚ    attr_namer:r:r;Ú build_attribss
 
 zBaseXMLFormatter.build_attribsz str | tuple)r`r-cCsT|}t|tƒrFd|kr.d dd„|Dƒ¡ ¡nd dd„|Dƒ¡ ¡}|j›|›S)NrPcSsg|] }t|ƒ‘qSr:©r©rHÚcr:r:r;rJsz7BaseXMLFormatter._get_flat_col_name.<locals>.<listcomp>Ú_cSsg|] }t|ƒ‘qSr:rcrdr:r:r;rJs)Ú
isinstanceÚtupleÚjoinÚstripr7)r9r`Zflat_colr:r:r;r]s
ÿýz#BaseXMLFormatter._get_flat_col_namecCs t|ƒ‚dS)z¼
        Create child elements of row.
 
        This method adds child elements using elem_cols to row element and
        works with tuples for multindex or hierarchical columns.
        Nr
)r9rZr[r:r:r;Ú build_elems#szBaseXMLFormatter.build_elemsc    Cs€|js
dS|jD]j}| |¡}z8t||ƒs8||dkr<dn
t||ƒ}||||ƒ_Wqtk
rxtd|›ƒ‚YqXqdS)NrPr\)r%r]rrÚtextr_)r9Zsub_element_clsrZr[r`Z    elem_nameÚvalr:r:r;Ú _build_elems-s
 
(zBaseXMLFormatter._build_elemsc    CsZ| ¡}|jdk    rFt|jd|j|jdd}|j |¡W5QRXdS| |j¡     ¡SdS)NÚwbF)r,rZis_text)
rArrr,rÚhandleÚwriteÚdecoder(Úrstrip)r9Zxml_docZhandlesr:r:r;Ú write_output9s
ûzBaseXMLFormatter.write_output)NTrrNNNNNrTTNrN)rDÚ
__module__Ú __qualname__Ú__doc__r<rAr4r5r2r8r6rWrbr]rkrnrtr:r:r:r;r&s:>ï0,    
 
 rc@sheZdZdZddœdd„Zddœdd„Zd    d
d d œd d„Zddœdd„Zddœdd„Zddœdd„Z    dS)ÚEtreeXMLFormatterú„
    Class for formatting data in xml using Python standard library
    modules: `xml.etree.ElementTree` and `xml.dom.minidom`.
    r=r>cCsòddlm}m}m}||j›|j›| ¡d|_|j     ¡D]\}||j|j›|j
›ƒ}|j s€|j s€t | ¡ƒ|_ | ||¡q<| ||¡}| ||¡q<||jd|jd|_|jr¾| ¡|_|jrÐ| ¡|_n
| ¡|_|jdk    rìtdƒ‚|jS)Nr©ÚElementÚ
SubElementÚtostring)r^Úxml)Úmethodr(zBTo use stylesheet, you need lxml installed and selected as parser.)Úxml.etree.ElementTreer{r|r}r7r!rWÚrootr3Úvaluesr"r$r%ÚlistrMrkrbr(Úout_xmlr*Ú prettify_treer)Úadd_declarationÚremove_declarationr+Ú
ValueError©r9r{r|r}rZr[r:r:r;rAQs.ÿ  
 
 
ÿzEtreeXMLFormatter.build_treercCs¤ddlm}d}|jr |j ¡D]&\}}t|tƒr t|tƒr |||ƒq |jrŽzd|j|j›d}Wq tk
rŠt|j›dƒ‚Yq Xnd|jd›d}|S)Nr)Úregister_namespacerPÚ{Ú}ú is not included in namespaces)r€rŠr&rVrgrr'r_)r9rŠÚurirSrTr:r:r;r6xs  z EtreeXMLFormatter.get_prefix_urirXrrrYcCsddlm}| |||¡dS©Nr)r|)r€r|rn©r9rZr[r|r:r:r;rkŠs zEtreeXMLFormatter.build_elemscCs&ddlm}||jƒ}|jd|jdS)zƒ
        Output tree for pretty print format.
 
        This method will pretty print xml with line breaks and indentation.
        r)Ú parseStringz  )Úindentr()Zxml.dom.minidomr‘r„Z toprettyxmlr()r9r‘Údomr:r:r;r…s 
zEtreeXMLFormatter.prettify_treecCs2d|j›d}|j d¡r |jS| |j¡|jS)z¶
        Add xml declaration.
 
        This method will add xml declaration of working tree. Currently,
        xml_declaration is supported in etree starting in Python 3.8.
        z<?xml version="1.0" encoding="z"?>
s<?xml)r(r„Ú
startswithÚencode)r9Údeclr:r:r;r†œs
 
ÿýz!EtreeXMLFormatter.add_declarationcCs|j d¡d ¡S)z¦
        Remove xml declaration.
 
        This method will remove xml declaration of working tree. Currently,
        pretty_print is not supported in etree.
        s?>éÿÿÿÿ)r„Úsplitrjr@r:r:r;r‡«sz$EtreeXMLFormatter.remove_declarationN)
rDrurvrwrAr6rkr…r†r‡r:r:r:r;rxKs' rxcspeZdZdZddœ‡fdd„ Zddœdd„Zddœd    d
„Zd dœd d „Zddddœdd„Zddœdd„Z    ‡Z
S)ÚLxmlXMLFormatterryrr>cstƒj||Ž| ¡dSr.)Úsuperr<Úconvert_empty_str_key)r9ÚargsÚkwargs©Ú    __class__r:r;r<¼szLxmlXMLFormatter.__init__r=cCsÎddlm}m}m}||j›|j›|jd|_|j     ¡D]\}||j|j›|j
›ƒ}|j s~|j s~t | ¡ƒ|_ | ||¡q:| ||¡}| ||¡q:||j|jd|j|jd|_|jdk    rÈ| ¡|_|jS)r?rrz)Znsmapr~)r*rr(r)N)Ú
lxml.etreer{r|r}r7r!r&rr3r‚r"r$r%rƒrMrkrbr*r(r)r„r+Ú transform_docr‰r:r:r;rAÁs&  û
 
zLxmlXMLFormatter.build_treecCs,|jr(d|j ¡kr(|j dd¡|jd<dS)z¿
        Replace zero-length string in `namespaces`.
 
        This method will replace '' with None to align to `lxml`
        requirement that empty string prefixes are not allowed.
        rPÚdefaultN)r&rMÚpopr@r:r:r;r›èsz&LxmlXMLFormatter.convert_empty_str_keyrcCsfd}|jrb|jrPzd|j|j›d}Wqbtk
rLt|j›dƒ‚YqbXnd|jd›d}|S)NrPr‹rŒr)r&r'r_)r9rŽr:r:r;r6ószLxmlXMLFormatter.get_prefix_urirXrrYcCsddlm}| |||¡dSr)r r|rnrr:r:r;rks zLxmlXMLFormatter.build_elemsc     Cs¬ddlm}m}m}m}|j}|dk    s*t‚t||j|j    |j
d}t |ƒD}||jd}t |t jƒr||| ¡ |j¡|d}    n |||d}    W5QRX||    ƒ}
|
|jƒ} t| ƒS)zõ
        Parse stylesheet from file or buffer and run it.
 
        This method will parse stylesheet object into tree for parsing
        conditionally by its specific object type, then transforms
        original tree with XSLT script.
        r)ÚXSLTÚ    XMLParserÚ
fromstringÚparseN)Zfilepath_or_bufferr(r,r)r()Úparser)r r¤r¥r¦r§r+ÚAssertionErrorrr(r,rrrgÚioÚStringIOÚgetvaluer•rr=) r9r¤r¥r¦r§Z    style_docÚ handle_dataÚxml_dataZ curr_parserZxsl_docZ transformerZnew_docr:r:r;r¡s( ü
  ÿ
zLxmlXMLFormatter.transform_doc) rDrurvrwr<rAr›r6rkr¡Ú __classcell__r:r:ržr;r™¶s'  r™)"rwÚ
__future__rrErªÚtypingrrZpandas._typingrrrrr    Z pandas.errorsr Zpandas.util._decoratorsr Zpandas.core.dtypes.commonr Zpandas.core.dtypes.missingrZpandas.core.shared_docsrZpandas.io.commonrZ pandas.io.xmlrrZpandasrrrxr™r:r:r:r;Ú<module>s.        
þ#k