
    F\hJ                     D   S r SSKrSSKrSSKJrJrJrJrJrJ	r	J
r
JrJrJrJrJrJrJr  SSKJr  SSKJr  / SQrSr " S S	5      r " S
 S\5      r\" S\5         " S S\5      r\" S\5         " S S\5      r\" S\5         " S S5      r " S S5      r " S S5      rg)a)  
CSV parsing and writing.

This module provides classes that assist in the reading and writing
of Comma Separated Value (CSV) files, and implements the interface
described by PEP 305.  Although many CSV files are simple to parse,
the format is not formally defined by a stable specification and
is subtle enough that parsing lines of a CSV file with something
like line.split(",") is bound to fail.  The module supports three
basic APIs: reading, writing, and registration of dialects.


DIALECT REGISTRATION:

Readers and writers support a dialect argument, which is a convenient
handle on a group of settings.  When the dialect argument is a string,
it identifies one of the dialects previously registered with the module.
If it is a class or instance, the attributes of the argument are used as
the settings for the reader or writer:

    class excel:
        delimiter = ','
        quotechar = '"'
        escapechar = None
        doublequote = True
        skipinitialspace = False
        lineterminator = '\r\n'
        quoting = QUOTE_MINIMAL

SETTINGS:

    * quotechar - specifies a one-character string to use as the
        quoting character.  It defaults to '"'.
    * delimiter - specifies a one-character string to use as the
        field separator.  It defaults to ','.
    * skipinitialspace - specifies how to interpret spaces which
        immediately follow a delimiter.  It defaults to False, which
        means that spaces immediately following a delimiter is part
        of the following field.
    * lineterminator - specifies the character sequence which should
        terminate rows.
    * quoting - controls when quotes should be generated by the writer.
        It can take on any of the following module constants:

        csv.QUOTE_MINIMAL means only when required, for example, when a
            field contains either the quotechar or the delimiter
        csv.QUOTE_ALL means that quotes are always placed around fields.
        csv.QUOTE_NONNUMERIC means that quotes are always placed around
            fields which do not parse as integers or floating-point
            numbers.
        csv.QUOTE_STRINGS means that quotes are always placed around
            fields which are strings.  Note that the Python value None
            is not a string.
        csv.QUOTE_NOTNULL means that quotes are only placed around fields
            that are not the Python value None.
        csv.QUOTE_NONE means that quotes are never placed around fields.
    * escapechar - specifies a one-character string used to escape
        the delimiter when quoting is set to QUOTE_NONE.
    * doublequote - controls the handling of quotes inside fields.  When
        True, two consecutive quotes are interpreted as one during read,
        and when writing, each quote character embedded in the data is
        written as two quotes
    N)Errorwriterreaderregister_dialectunregister_dialectget_dialectlist_dialectsfield_size_limitQUOTE_MINIMAL	QUOTE_ALLQUOTE_NONNUMERIC
QUOTE_NONEQUOTE_STRINGSQUOTE_NOTNULL)Dialect)StringIO)r   r   r   r   r   r   r   r   excel	excel_tabr
   r   r   r   r   r	   Snifferr   
DictReader
DictWriterunix_dialectz1.0c                   H    \ rS rSrSrSrSrSrSrSr	Sr
SrSrSrS rS rSrg)	r   X   zDescribe a CSV dialect.

This must be subclassed (see csv.excel).  Valid attributes are:
delimiter, quotechar, escapechar, doublequote, skipinitialspace,
lineterminator, quoting.

 FNc                 Z    U R                   [        :w  a  SU l        U R                  5         g )NT)	__class__r   _valid	_validateselfs    /usr/lib/python3.13/csv.py__init__Dialect.__init__k   s    >>W$DK    c                 j     [        U 5        g ! [         a  n[        [        U5      5      S eS nAff = fN)_Dialect	TypeErrorr   str)r!   es     r"   r   Dialect._validatep   s.    	*TN 	*A-T)	*s    
2-2)r   )__name__
__module____qualname____firstlineno____doc___namer   	delimiter	quotechar
escapechardoublequoteskipinitialspacelineterminatorquotingr#   r   __static_attributes__ r%   r"   r   r   X   sA     EFIIJKNG
*r%   r   c                   0    \ rS rSrSrSrSrSrSrSr	\
rSrg	)
r   w   z;Describe the usual properties of Excel-generated CSV files.,"TF
r;   N)r-   r.   r/   r0   r1   r3   r4   r6   r7   r8   r   r9   r:   r;   r%   r"   r   r   w   s$    EIIKNGr%   r   c                       \ rS rSrSrSrSrg)r      zEDescribe the usual properties of Excel-generated TAB-delimited files.	r;   N)r-   r.   r/   r0   r1   r3   r:   r;   r%   r"   r   r      s
    OIr%   r   z	excel-tabc                   0    \ rS rSrSrSrSrSrSrSr	\
rSrg	)
r      z:Describe the usual properties of Unix-generated CSV files.r>   r?   TF
r;   N)r-   r.   r/   r0   r1   r3   r4   r6   r7   r8   r   r9   r:   r;   r%   r"   r   r      s$    DIIKNGr%   r   unixc                       \ rS rSr  S	S jrS r\S 5       r\R                  S 5       rS r	\
" \R                  5      rSrg)
r      Nc                     Ub  [        U5      UL a  [        U5      nX l        X0l        X@l        [        X/UQ70 UD6U l        XPl        SU l        g Nr   )iterlist_fieldnamesrestkeyrestvalr   dialectline_num)r!   f
fieldnamesrO   rP   rQ   argskwdss           r"   r#   DictReader.__init__   sP    !d:&6*&Dj)J%Q7$7$7r%   c                     U $ r'   r;   r    s    r"   __iter__DictReader.__iter__   s    r%   c                     U R                   c   [        U R                  5      U l         U R                  R                  U l        U R                   $ ! [         a     N3f = fr'   )rN   nextr   StopIterationrR   r    s    r"   rT   DictReader.fieldnames   sW    ##'#4  ,, ! s   A 
AAc                     Xl         g r'   )rN   )r!   values     r"   rT   r^      s     r%   c                    U R                   S:X  a  U R                    [        U R                  5      nU R                  R                   U l         U/ :X  a  [        U R                  5      nU/ :X  a  M  [	        [        U R                  U5      5      n[        U R                  5      n[        U5      nX4:  a  XS  X R                  '   U$ X4:  a$  U R                  US   H  nU R                  X%'   M     U$ rK   )	rR   rT   r\   r   dictziplenrO   rP   )r!   rowdlflrkeys         r"   __next__DictReader.__next__   s    ==AOO4;;,,
 Rit{{#C RiT__c*+!X7!#hAllO  Wrs+ ,r%   )rN   rQ   rR   r   rO   rP   )NNNr   )r-   r.   r/   r0   r#   rY   propertyrT   setterrj   classmethodtypesGenericAlias__class_getitem__r:   r;   r%   r"   r   r      sX    AE 	     ! !, $E$6$67r%   r   c                   ^    \ rS rSr  S	S jrS rS rS rS r\	" \
R                  5      rSrg)
r      c                     Ub  [        U5      UL a  [        U5      nX l        X0l        UR	                  5       nUS;  a  [        SU-  5      eX@l        [        X/UQ70 UD6U l        g )N)raiseignorez-extrasaction (%s) must be 'raise' or 'ignore')rL   rM   rT   rP   lower
ValueErrorextrasactionr   )r!   rS   rT   rP   ry   rQ   rU   rV   s           r"   r#   DictWriter.__init__   ss    !d:&6*&Dj)J$#))+22L+, - -(Q7$7$7r%   c                 v    [        [        U R                  U R                  5      5      nU R                  U5      $ r'   )rb   rc   rT   writerow)r!   headers     r"   writeheaderDictWriter.writeheader   s*    c$//4??;<}}V$$r%   c                   ^ ^ T R                   S:X  a[  TR                  5       T R                  -
  nU(       a7  [        SSR	                  U Vs/ s H  n[        U5      PM     sn5      -   5      eUU 4S jT R                   5       $ s  snf )Nru   z(dict contains fields not in fieldnames: z, c              3   \   >#    U  H!  nTR                  UTR                  5      v   M#     g 7fr'   )getrP   ).0ri   rowdictr!   s     r"   	<genexpr>+DictWriter._dict_to_list.<locals>.<genexpr>   s#     J/3C../s   ),)ry   keysrT   rx   joinrepr)r!   r   wrong_fieldsxs   ``  r"   _dict_to_listDictWriter._dict_to_list   st    '"<<>DOO;L !K#'99|-L|!d1g|-L#M"N O OJ$//JJ .Ms   Bc                 V    U R                   R                  U R                  U5      5      $ r'   )r   r|   r   )r!   r   s     r"   r|   DictWriter.writerow   s"    {{##D$6$6w$?@@r%   c                 `    U R                   R                  [        U R                  U5      5      $ r'   )r   	writerowsmapr   )r!   rowdictss     r"   r   DictWriter.writerows   s$    {{$$S););X%FGGr%   )ry   rT   rP   r   N)r   ru   r   )r-   r.   r/   r0   r#   r~   r   r|   r   rn   ro   rp   rq   r:   r;   r%   r"   r   r      s7    ?F 8%KAH $E$6$67r%   r   c                   :    \ rS rSrSrS rS
S jrS rS rS r	S	r
g)r      zY
"Sniffs" the format of a CSV file (i.e. delimiter, quotechar)
Returns a Dialect object.
c                     / SQU l         g )N)r>   rC   ; :	preferredr    s    r"   r#   Sniffer.__init__   s	    3r%   Nc                     U R                  X5      u  p4pVU(       d  U R                  UU5      u  pVU(       d  [        S5      e " S S[        5      nXGl        XWl        U=(       d    SUl        Xgl        U$ )z9
Returns a dialect (or None) corresponding to the sample
zCould not determine delimiterc                        \ rS rSrSrSr\rSrg)Sniffer.sniff.<locals>.dialecti  sniffedr@   r;   N)	r-   r.   r/   r0   r2   r8   r   r9   r:   r;   r%   r"   rQ   r     s    E#N#Gr%   rQ   r?   )_guess_quote_and_delimiter_guess_delimiterr   r   r6   r3   r4   r7   )r!   sample
delimitersr4   r6   r3   r7   rQ   s           r"   sniffSniffer.sniff   sz     226F 	<		*.*?*?@J+L'I 788	$g 	$ *%%,#3 r%   c                 r   / nS HS  n[         R                  " U[         R                  [         R                  -  5      nUR	                  U5      nU(       d  MS    O   U(       d  g0 n0 nSnWR
                  n	U Hy  n
U	S   S-
  nX   nU(       a  UR                  US5      S-   Xl'    U	S   S-
  nX   nU(       a  Ub  X;   a  UR                  US5      S-   X|'    U	S   S-
  nX   (       d  Mt  US-  nM{     [        XfR                  S9nU(       a#  [        XwR                  S9nX~   U:H  nUS	:X  a  S
nOS
nSn[         R                  " S[         R                  " U5      US.-  [         R                  5      nUR                  U5      (       a  SnOSnUUX4$ ! [         a     GM+  f = f! [         a     GM=  f = f)aG  
Looks for text enclosed between two identical quotes
(the probable quotechar) which are preceded and followed
by the same character (the probable delimiter).
For example:
                 ,'some text',
The quote with the most wins, same with the delimiter.
If there is no quotechar the delimiter can't be determined
this way.
)zI(?P<delim>[^\w\n"\'])(?P<space> ?)(?P<quote>["\']).*?(?P=quote)(?P=delim)zG(?:^|\n)(?P<quote>["\']).*?(?P=quote)(?P<delim>[^\w\n"\'])(?P<space> ?)zG(?P<delim>[^\w\n"\'])(?P<space> ?)(?P<quote>["\']).*?(?P=quote)(?:$|\n)z-(?:^|\n)(?P<quote>["\']).*?(?P=quote)(?:$|\n))r   FNr   r   quote   delimspaceri   rF   r   z]((%(delim)s)|^)\W*%(quote)s[^%(delim)s\n]*%(quote)s[^%(delim)s\n]*%(quote)s\W*((%(delim)s)|$))r   r   TF)recompileDOTALL	MULTILINEfindall
groupindexr   KeyErrormaxescapesearch)r!   datar   matchesrestrregexpquotesdelimsspacesr   mnri   r4   r   r7   	dq_regexpr6   s                     r"   r   "Sniffer._guess_quote_and_delimiter  s    HE ZZryy2<<'?@FnnT*GwH '&&
A7#a'A$C$jja014w'!+d 
*c.?$jja014w'!+ tt!# & JJ/	JJ/E%}6} E  JJ(*		%(8)L MNPll\	 D!!KK;@@I    s$   &FF'
F$#F$'
F65F6c                    [        [        SUR                  S5      5      5      n[        S5       Vs/ s H  n[	        U5      PM     nn[        S[        U5      5      nSn0 n0 n0 n	SUpU
[        U5      :  Ga  US-  nXU  HJ  nU HA  nUR                  U0 5      nUR                  U5      nUR                  US5      S-   X'   XU'   MC     ML     UR                  5        H  n[        X}   R                  5       5      n[        U5      S:X  a  US   S   S:X  a  M;  [        U5      S:  aE  [        US S9X'   UR                  X   5        X   S   X   S   [        S	 U 5       5      -
  4X'   M  US   X'   M     UR                  5       n[        [        XV-  [        U5      5      5      nS
nSn[        U	5      S:X  ab  UU:  a\  U H:  u  nnUS   S:  d  M  US   S:  d  M  US   U-  U:  d  M*  Ub  UU;   d  M5  UU	U'   M<     US-  n[        U	5      S:X  a  UU:  a  M\  [        U	5      S:X  aL  [        U	R                  5       5      S   nUS   R                  U5      US   R                  SU-  5      :H  nUU4$ Un
X-  nU
[        U5      :  a  GM  U	(       d  g[        U	5      S:  aY  U R                   HI  nUU	R                  5       ;   d  M  US   R                  U5      US   R                  SU-  5      :H  nUU4s  $    U	R                  5        VVs/ s H
  u  nnUU4PM     nnnUR!                  5         US   S   nUS   R                  U5      US   R                  SU-  5      :H  nUU4$ s  snf s  snnf )a=  
The delimiter /should/ occur the same number of times on
each row. However, due to malformed data, it may not. We don't want
an all or nothing approach, so we allow for small variations in this
number.
  1) build a table of the frequency of each character on every line.
  2) build a table of frequencies of this frequency (meta-frequency?),
     e.g.  'x occurred 5 times in 10 rows, 6 times in 1000 rows,
     7 times in 2 rows'
  3) use the mode of the meta-frequency to determine the /expected/
     frequency for that character
  4) find out how often the character actually meets that goal
  5) the character that best meets its goal is the delimiter
For performance reasons, the data is evaluated in chunks, so it can
try and evaluate the smallest portion of the data possible, evaluating
additional chunks as necessary.
NrF      
   r   r   c                     U S   $ )Nr   r;   )r   s    r"   <lambda>*Sniffer._guess_delimiter.<locals>.<lambda>  s    1Q4r%   r   c              3   *   #    U  H	  oS    v   M     g7f)r   Nr;   )r   items     r"   r   +Sniffer._guess_delimiter.<locals>.<genexpr>  s     (CUTaUs   g      ?g?g{Gz?z%c )r   r   )rM   filtersplitrangechrminrd   r   countr   itemsr   removesumfloatr   sort)r!   r   r   casciichunkLength	iterationcharFrequencymodesr   startendlinecharmetaFrequencyfreqr   modeListtotalconsistency	thresholdkvr   r7   rf   s                             r"   r   Sniffer._guess_delimiter]  s   & F4D!123!&s,AQ, "c$i(	sc$iNI3!D$1$5$5dB$?M::d+D*7*;*;D!*Dq*HM'*7$' " ( &**,]06689u:?uQx{a'7u:>"%e"@EK LL-#(;q>5;q>%((CU(C%C4D #EEK #((EK -  {{}H#k5s4yABEKIf+"{i'?$DAqtaxAaD1HqT%ZK7'/1
?()F1I	 %
 t# f+"{i'? 6{aV[[]+A.$(GMM%$8$(GMM%%-$@%A /00 ECc c$if  v;?^^%(,Qa(8(,Qeai(@)A$/00	 $ %+LLN3N5Aa!AN3

b	! GMM%0 GMM%%-89'((_ -R 4s   M1M6c                    [        [        U5      U R                  U5      5      n[        U5      n[	        U5      n0 n[        U5       H  nS XV'   M	     SnU Hi  nUS:  a    ObUS-  n[	        U5      U:w  a  M!  [        UR                  5       5       H+  n	[        n
 U
" X   5        XU	   :w  d  M  XY   c  XU	'   M)  XY	 M-     Mk     SnUR                  5        HI  u  p[        U[        5      (       a  [	        X9   5      U:w  a  US-  nM2  US-  nM9   U" X9   5        US-  nMK     US:  $ ! [        [        4 a    [	        X   5      n
 Nf = f! [        [        4 a	    US-  n M  f = f)Nr      r   )r   r   r   r\   rd   r   rM   r   complexrx   OverflowErrorr   
isinstanceintr)   )r!   r   rdrr}   columnscolumnTypesicheckedre   colthisType	hasHeadercolTypes                r"   
has_headerSniffer.has_header  sx    Xf%tzz&'9:cf+wA$C|qLG3x7"K,,./"-SX&
 3//"'/+3C( (, 0 6 	'--/LC'3''v{#w.NINI#FK( NI 0 1}; #M2 -"38}H-0 #I. #NI#s$   
D(
E(EEE$#E$r   r'   )r-   r.   r/   r0   r1   r#   r   r   r   r   r:   r;   r%   r"   r   r      s%    4
:IAXd)N=r%   r   )r1   r   ro   _csvr   r   r   r   r   r   r	   r
   r   r   r   r   r   r   r   r(   ior   __all____version__r   r   r   r   r   r   r;   r%   r"   <module>r      s   >@ 
 . . . .
 %  * *>G  %    i (7   &38 38l 8  8FW Wr%   