Jz`fJcsDSrSSKrSSKrSSKJrJrJrJrJrJ 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 r r rrrexcel excel_tabrrrrrrSnifferr DictReader DictWriter unix_dialectz1.0csH\rSrSrSrSrSrSrSrSr Sr Sr Sr Sr SrSrSrg) riXzDescribe a CSV dialect. This must be subclassed (see csv.excel). Valid attributes are: delimiter, quotechar, escapechar, doublequote, skipinitialspace, lineterminator, quoting. FNcsZUR[:waSUlUR5 g)NT) __class__r_valid _validateself /usr/lib64/python3.13/csv.py__init__ZDialect.__init__ks >>W $DK csj[U5 g![an[[U55SeSnAff=fN)_Dialect TypeErrorrZstr)rZe rrZDialect._validateps. * TN *A-T ) *s  2-2)rr)__name__ __module__ __qualname____firstlineno____doc___namer delimiter quotecharZ escapechar doublequoteskipinitialspacelineterminatorquotingrr__static_attributes__r rrrXsA E FIIJKNG *r rc0\rSrSrSrSrSrSrSrSr \ r Sr g ) riwz;Describe the usual properties of Excel-generated CSV files.,"TF r2N) r%r&r'r(r)r+r,r-r.r/r r0r1r2r rrrws$EIIKNGr rcs\rSrSrSrSrSrg)rizEDescribe the usual properties of Excel-generated TAB-delimited files. r2N)r%r&r'r(r)r+r1r2r rrrs OIr rz excel-tabcr3) riz:Describe the usual properties of Unix-generated CSV files.r4r5TF r2N) r%r&r'r(r)r+r,r-r.r/r r0r1r2r rrrs$DIIKNGr rZunixcs\rSrSrS SjrSr\S5r\RS5rSr \ "\ R5r Srg) riNcsUb[U5ULa [U5nX lX0lX@l[ X/UQ70UD6UlXPlSUlgNr)iterlist _fieldnamesrestkeyrestvalrdialectline_num)rf fieldnamesr=r>r?argskwds rrZDictReader.__init__sP  !d:&6*&Dj)J%  Q7$7$7   r csU$r!r2rrr__iter__ZDictReader.__iter__s r csURc[UR5UlURRUlUR$![a N3f=fr!)r<nextrZ StopIterationr@rrrrBDictReader.fieldnamessW    # #' #4  ,, !  sA AAcsXlgr!)r<)rZvaluer$rrBrHs r csURS:Xa UR [UR5nURRUlU/:Xa[UR5nU/:XaM[ [ URU55n[ UR5n[ U5nX4:aXSX R'U$X4:a$URUSHnURX%'M U$r9) r@rBrGrdictziplenr=r>)rrowdZlfZlrkeys r__next__ZDictReader.__next__s ==A  OO4;; ,, Rit{{#CRi T__c* +  ! X 7!#hAllOWrs+ & &r )rr?r@r<r>r=rB)NNNr)r%r&r'r(rrFZpropertyrBZsetterrO classmethodtypes GenericAlias__class_getitem__r1r2r rrrsXAE    !!,$E$6$67r rcs^\rSrSrS SjrSrSrSrSr\ "\ R5r Sr g) ricsUb[U5ULa [U5nX lX0lUR 5nUS;a[ SU-5eX@l[X/UQ70UD6Ulg)N)raiseZignorez-extrasaction (%s) must be 'raise' or 'ignore')r:r;rBr>Zlower ValueError extrasactionr)rrArBr>rVr?rCrDrErrZDictWriter.__init__ss  !d:&6*&Dj)J$ #))+ 2 2L+,- -(Q7$7$7 r csv[[URUR55nURU5$r!)rIrJrBwriterow)rheaderr$r writeheaderZDictWriter.writeheaders*c$//4??;<}}V$$r c s^^TRS:XaZTR5TR- nU(a6[SSR UVs/sHn[ U5PM sn5-5eUU4SjTR5$s snf)NrTz(dict contains fields not in fieldnames: z, c3sZ># UH!nTRUTR5v M# g7fr!)getr>).0rNrowdictrs r Z+DictWriter._dict_to_list..s"J3 C..Js(+)rVkeysrBrUZjoinZrepr)rr\Z wrong_fieldsxs`` r _dict_to_listZDictWriter._dict_to_listsp    '"<<>DOO;L !K#'99|-L!d1g-L#M"NOOJ$//JJ.Ms BcsVURRURU55$r!)rrWr`)rr\r$rrWZDictWriter.writerows"{{##D$6$6w$?@@r cs`URR[URU55$r!)r writerowsZmapr`)rZrowdictsr$rraZDictWriter.writerowss${{$$S););X%FGGr )r`r>rBrVrN)rrTr)r%r&r'r(rrYr`rWrarPrQrRrSr1r2r rrrs7?F  8%KAH$E$6$67r rcs:\rSrSrSrSrS SjrSrSrSr S r g) rizY "Sniffs" the format of a CSV file (i.e. delimiter, quotechar) Returns a Dialect object. cs/SQUlg)N)r4r7Z;Z Z: preferredrrrrZSniffer.__init__s 3r NcsURX5up4pVU(dURUU5upVU(d [S5e"SS[5nXGlXWlU=(d SUlXglU$)z9 Returns a dialect (or None) corresponding to the sample zCould not determine delimitercs \rSrSrSrSr\rSrg)Sniffer.sniff..dialectiZsniffedr6r2N) r%r&r'r(r*r/r r0r1r2r rr?rdsE#N#Gr r?r5)_guess_quote_and_delimiter_guess_delimiterrrr-r+r,r.)rsample delimitersr,r-r+r.r?rErsniffZ Sniffer.sniffsz 226F < *.*?*?@J+L 'I78 8 $g $ *%%,#3 r csr/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))rFNrrquotedelimZspacerNr8rz]((%(delim)s)|^)\W*%(quote)s[^%(delim)s\n]*%(quote)s[^%(delim)s\n]*%(quote)s\W*((%(delim)s)|$))rlrjTF) reZcompileZDOTALLZ MULTILINEZfindall groupindexrZZKeyErrormaxZescapeZsearch)rdatarhZmatchesZrestrZregexpZquotesdelimsZspacesroZmZnrNr,rlr.Z dq_regexpr-s rreZ"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$' F65F6cs[[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. Nr8ii rrkcs US$)Nrkr2)r_rrZZ*Sniffer._guess_delimiter..s 1Q4r rmc3s(# UH oSv M g7f)rkNr2)r[Zitemr$rr]Z+Sniffer._guess_delimiter..s(CTa(Csg?g?g{Gz?z%c )rri)r;ZfilterZsplitrangeZchrZminrKrZZcountr^itemsrpZremoveZsumZfloatrcZsort)rrqrhZcZasciiZ chunkLengthZ iterationZ charFrequencyZmodesrrZstartZendZlineZcharZ metaFrequencyZfreqrtZmodeListZtotalZ consistencyZ thresholdZkZvrlr.rMs rrfZSniffer._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/M4cs[[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)Nrirk)rrrirGrKrsr;r^ZcomplexrUZ OverflowErrorrtZ isinstanceZintr#) rrgZrdrrXZcolumnsZ columnTypesZiZcheckedrLZcolZthisTypeZ hasHeaderZcolTypes r has_headerZSniffer.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$rbr!) r%r&r'r(r)rrirerfrur1r2r rrrs%4 :IAXd)N=r r)r)rnrQZ_csvrrrrrrrrr r r r r rrr"ZiorZ__all__Z __version__rrrrrrr2r rrvs>@ .... %  **>G% i(7&3838l 8 8FWWr