Jz`fJDSrSSKrSSKrSSKJrJrJrJrJrJ r J r J r J r J r JrJrJrJr SSKJr SSKJr /SQrSr"SS 5r"S S \5r\"S \5 "S S \5r\"S\5 "SS\5r\"S\5 "SS5r"SS5r"SS5rg)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)Errorwriterreaderregister_dialectunregister_dialect get_dialect list_dialectsfield_size_limit QUOTE_MINIMAL QUOTE_ALLQUOTE_NONNUMERIC QUOTE_NONE QUOTE_STRINGS QUOTE_NOTNULL)Dialect)StringIO)r r r rrrrrexcel excel_tabr rrrrr Snifferr DictReader DictWriter unix_dialectz1.0cH\rSrSrSrSrSrSrSrSr Sr Sr Sr Sr SrSrSrg) rXzDescribe a CSV dialect. This must be subclassed (see csv.excel). Valid attributes are: delimiter, quotechar, escapechar, doublequote, skipinitialspace, lineterminator, quoting. FNcZUR[:waSUlUR5 g)NT) __class__r_valid _validateselfs /usr/lib64/python3.13/csv.py__init__Dialect.__init__ks >>W $DK cj[U5 g![an[[U55SeSnAff=fN)_Dialect TypeErrorrstr)r!es r"rDialect._validateps. * TN *A-T ) *s  2-2)rr)__name__ __module__ __qualname____firstlineno____doc___namer delimiter quotechar escapechar doublequoteskipinitialspacelineterminatorquotingr#r__static_attributes__r%r"rrXsA E FIIJKNG *r%rc0\rSrSrSrSrSrSrSrSr \ r Sr g ) rwz;Describe the usual properties of Excel-generated CSV files.,"TF r;N) r-r.r/r0r1r3r4r6r7r8r r9r:r;r%r"rrws$EIIKNGr%rc\rSrSrSrSrSrg)rzEDescribe the usual properties of Excel-generated TAB-delimited files. r;N)r-r.r/r0r1r3r:r;r%r"rrs OIr%rz excel-tabc0\rSrSrSrSrSrSrSrSr \ r Sr g ) rz:Describe the usual properties of Unix-generated CSV files.r>r?TF r;N) r-r.r/r0r1r3r4r6r7r8r r9r:r;r%r"rrs$DIIKNGr%runixc\rSrSrS SjrSr\S5r\RS5rSr \ "\ R5r Srg) rNcUb[U5ULa [U5nX lX0lX@l[ X/UQ70UD6UlXPlSUlgNr)iterlist _fieldnamesrestkeyrestvalrdialectline_num)r!f fieldnamesrOrPrQargskwdss r"r#DictReader.__init__sP  !d:&6*&Dj)J%  Q7$7$7   r%cU$r'r;r s r"__iter__DictReader.__iter__s r%cURc[UR5UlURRUlUR$![a N3f=fr')rNnextr StopIterationrRr s r"rTDictReader.fieldnamessW    # #' #4  ,, !  sA AAcXlgr')rN)r!values r"rTr^s r%cURS:Xa UR [UR5nURRUlU/:Xa[UR5nU/:XaM[ [ URU55n[ UR5n[ U5nX4:aXSX R'U$X4:a$URUSHnURX%'M U$rK) rRrTr\rdictziplenrOrP)r!rowdlflrkeys r"__next__DictReader.__next__s ==A  OO4;; ,, Rit{{#CRi T__c* +  ! X 7!#hAllOWrs+ & &r%)rrQrRrNrPrOrT)NNNr)r-r.r/r0r#rYpropertyrTsetterrj classmethodtypes GenericAlias__class_getitem__r:r;r%r"rrsXAE    !!,$E$6$67r%rc^\rSrSrS SjrSrSrSrSr\ "\ R5r Sr g) rcUb[U5ULa [U5nX lX0lUR 5nUS;a[ SU-5eX@l[X/UQ70UD6Ulg)N)raiseignorez-extrasaction (%s) must be 'raise' or 'ignore')rLrMrTrPlower ValueError extrasactionr)r!rSrTrPryrQrUrVs r"r#DictWriter.__init__ss  !d:&6*&Dj)J$ #))+ 2 2L+,- -(Q7$7$7 r%cv[[URUR55nURU5$r')rbrcrTwriterow)r!headers r" writeheaderDictWriter.writeheaders*c$//4??;<}}V$$r%c ^^TRS:XaZTR5TR- nU(a6[SSR UVs/sHn[ U5PM sn5-5eUU4SjTR5$s snf)Nruz(dict contains fields not in fieldnames: z, c3Z># UH!nTRUTR5v M# g7fr')getrP).0rirowdictr!s r" +DictWriter._dict_to_list..s"J3 C..Js(+)rykeysrTrxjoinrepr)r!r wrong_fieldsxs`` r" _dict_to_listDictWriter._dict_to_listsp    '"<<>DOO;L !K#'99|-L!d1g-L#M"NOOJ$//JJ.Ms BcVURRURU55$r')rr|r)r!rs r"r|DictWriter.writerows"{{##D$6$6w$?@@r%c`URR[URU55$r')r writerowsmapr)r!rowdictss r"rDictWriter.writerowss${{$$S););X%FGGr%)rrPrTryrN)rrur)r-r.r/r0r#r~rr|rrnrorprqr:r;r%r"rrs7?F  8%KAH$E$6$67r%rc:\rSrSrSrSrS SjrSrSrSr S r g) rzY "Sniffs" the format of a CSV file (i.e. delimiter, quotechar) Returns a Dialect object. c/SQUlg)N)r>rC; : preferredr s r"r#Sniffer.__init__s 3r%NcURX5up4pVU(dURUU5upVU(d [S5e"SS[5nXGlXWlU=(d SUlXglU$)z9 Returns a dialect (or None) corresponding to the sample zCould not determine delimiterc \rSrSrSrSr\rSrg)Sniffer.sniff..dialectisniffedr@r;N) r-r.r/r0r2r8r r9r:r;r%r"rQrsE#N#Gr%rQr?)_guess_quote_and_delimiter_guess_delimiterrrr6r3r4r7)r!sample delimitersr4r6r3r7rQs r"sniff Sniffer.sniffsz 226F < *.*?*?@J+L 'I78 8 $g $ *%%,#3 r%cr/nSHSn[R"U[R[R-5nUR U5nU(dMS O U(dg0n0nSnWR n UHyn U SS- n Xn U (aUR U S5S-Xl'U SS- n Xn U (aUbX;aUR U S5S-X|'U SS- n X(dMtUS- nM{ [XfR S9n U(a#[XwR S9nX~U:HnUS :XaS nOS nSn[R"S [R"U5U S .-[R5nURU5(aS nOSnU UX4$![a GM+f=f![a GM=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[^\w\n"\'])(?P ?)(?P["\']).*?(?P=quote)(?P=delim)zG(?:^|\n)(?P["\']).*?(?P=quote)(?P[^\w\n"\'])(?P ?)zG(?P[^\w\n"\'])(?P ?)(?P["\']).*?(?P=quote)(?:$|\n)z-(?:^|\n)(?P["\']).*?(?P=quote)(?:$|\n))rFNrrquotedelimspacerirFrz]((%(delim)s)|^)\W*%(quote)s[^%(delim)s\n]*%(quote)s[^%(delim)s\n]*%(quote)s\W*((%(delim)s)|$))rrTF) recompileDOTALL MULTILINEfindall groupindexrKeyErrormaxescapesearch)r!datarmatchesrestrregexpquotesdelimsspacesrmnrir4rr7 dq_regexpr6s r"r"Sniffer._guess_quote_and_delimitersH EZZryy2<<'?@FnnT*Gw '&&  A7#a'A$C$jja014  w'!+d *c.?$jja014  w'!+tt! # &JJ/ JJ/E%}6 }E JJ(* %(8)L MNPll\   D ! !KK;@@I     s$& FF' F$#F$' F65F6c[[SURS555n[S5Vs/sHn[ U5PM nn[ S[ U55nSn0n0n0n SUpU [ U5:GaUS- nXU HJn UHAn URU 05nU RU 5nURUS5S-X'XU 'MC ML UR5Hn [X}R55n[ U5S:XaUSSS:XaM;[ U5S:aE[USS9X'URX5 XSXS[S U55- 4X'MUSX'M UR5n[[ XV-[ U555nS nS n[ U 5S:XabUU:a\UH:unnUSS:dMUSS:dMUSU- U:dM*UbUU;dM5UU U'M< US -n[ U 5S:XaUU:aM\[ U 5S:XaL[U R55SnUSRU5USRS U-5:HnUU4$U n X- n U [ U5:aGMU (dg[ U 5S:aYURHInUU R5;dMUSRU5USRS U-5:HnUU4s $ U R5VVs/sH unnUU4PM nnnUR!5 USSnUSRU5USRS U-5:HnUU4$s snfs snnf)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 rrc US$)Nrr;)rs r"*Sniffer._guess_delimiter..s 1Q4r%rc3(# UH oSv M g7f)rNr;)ritems r"r+Sniffer._guess_delimiter..s(CTa(Csg?g?g{Gz?z%c )rr)rMfiltersplitrangechrminrdrcountritemsrremovesumfloatrsort)r!rrcascii chunkLength iteration charFrequencymodesrstartendlinechar metaFrequencyfreqrmodeListtotal consistency thresholdkvrr7rfs r"rSniffer._guess_delimiter]s&F4D!123!&s,AQ,,"c$i(    sc$i NI3 8!8D$1$5$5dB$?M::d+D*7*;*;D!*Dq*HM'*7$' 8 8&**, +]06689u:?uQx{a'7u:>"%e"@EKLL-#(;q>5;q>%((CU(C%C4D#EEK#((EK + {{}H#k5s4yABEKIf+"{i'?$*DAqtaxAaD1HqT%ZK7'/1 ?()F1I * t# f+"{i'?6{aV[[]+A.$(GMM%$8$(GMM%%-$@%A /00E  Ccc$if v;?^^ 1 %(,Q a(8(,Q eai(@)A$/00  1%+LLN35Aa!A33 b !  GMM%0 GMM%%-89'((_-R4s M/M4c[[U5URU55n[U5n[ U5n0n[ U5HnSXV'M SnUHinUS:a ObUS- n[ U5U:waM![ UR55H+n [n U "X5 XU :wdMXYcXU 'M)XY M- Mk Sn UR5HIup[U [5(a[ X95U :waU S- n M2U S-n M9U "X95 U S-n MK U S:$![[4a [ X5n Nf=f![[4a U S- n Mf=f)Nrr)rrrr\rdrrMrcomplexrx OverflowErrorr isinstanceintr)) r!rrdrr}columns columnTypesicheckedrecolthisType hasHeadercolTypes r" has_headerSniffer.has_headersXf%tzz&'9:cf+ w6A$6 -C| qLG3x7"K,,./ -"-SX& 3//"'/+3C((, - -6 '--/ #LC'3''v{#w.NINI#FK(NI #1};#M2-"38}H-0#I.#NI#s$ D( E (EE E$#E$rr') r-r.r/r0r1r#rrrrr:r;r%r"rrs%4 :IAXd)N=r%r)r1rro_csvrrrrrrr r r r r rrrrr(ior__all__ __version__rrrrrrr;r%r"rs>@ .... %  **>G% i(7&3838l 8 8FWWr%