[9?@sdZddlZddlmZmZmZmZmZmZm Z m Z m Z m Z m Z mZmZmZddlmZddlmZdddd d d d d ddddddddddddgZGdd d ZGdd d eZed eGdddeZedeGdddeZed eGd!ddZGd"ddZyeWnek reZYnXGd#ddZdS)$z+ csv.py - read/write/investigate CSV files N)Error __version__writerreaderregister_dialectunregister_dialect get_dialect list_dialectsfield_size_limit QUOTE_MINIMAL QUOTE_ALLQUOTE_NONNUMERIC QUOTE_NONE__doc__)Dialect)StringIOr r r rrrrexcel excel_tabr rrrrr Snifferrr DictReader DictWriterc@sdeZdZdZdZdZdZdZdZdZ dZ dZ dZ ddZ ddZdS) rzDescribe a CSV dialect. This must be subclassed (see csv.excel). Valid attributes are: delimiter, quotechar, escapechar, doublequote, skipinitialspace, lineterminator, quoting. FNcCs)|jtkrd|_n|jdS)NT) __class__r_valid _validate)selfr/usr/lib/python3.4/csv.py__init__)s zDialect.__init__cCsLyt|Wn7tk rG}ztt|WYdd}~XnXdS)N)_Dialect TypeErrorrstr)rerrrr.szDialect._validate)__name__ __module__ __qualname__r_namer delimiter quotecharZ escapechar doublequoteskipinitialspacelineterminatorquotingrrrrrrrs  c@s:eZdZdZdZdZdZdZdZe Z dS)rz;Describe the usual properties of Excel-generated CSV files.,"TFz N) r#r$r%rr'r(r)r*r+r r,rrrrr5s c@seZdZdZdZdS)rzEDescribe the usual properties of Excel-generated TAB-delimited files. N)r#r$r%rr'rrrrr?s z excel-tabc@s:eZdZdZdZdZdZdZdZe Z dS) unix_dialectz:Describe the usual properties of Unix-generated CSV files.r-r.TF N) r#r$r%rr'r(r)r*r+r r,rrrrr0Ds r0Zunixc@sgeZdZddddddZddZeddZejd dZd d ZdS) rNrcOsI||_||_||_t|||||_||_d|_dS)Nr) _fieldnamesrestkeyrestvalrdialectline_num)rf fieldnamesr3r4r5argskwdsrrrrPs     zDictReader.__init__cCs|S)Nr)rrrr__iter__YszDictReader.__iter__c CsS|jdkr=yt|j|_Wq=tk r9Yq=Xn|jj|_|jS)N)r2nextr StopIterationr6)rrrrr8\s zDictReader.fieldnamescCs ||_dS)N)r2)rvaluerrrr8fscCs|jdkr|jnt|j}|jj|_x|gkrXt|j}q:Wtt|j|}t|j}t|}||kr||d||js z,DictWriter._dict_to_list..z(dict contains fields not in fieldnames: z, cSsg|]}t|qSr)repr)rPxrrrrRs cs%g|]}j|jqSr)getr4)rPrD)rowdictrrrrRs )rLrKjoinr8)rrVZ wrong_fieldsr)rVrr _dict_to_lists &zDictWriter._dict_to_listcCs|jj|j|S)N)rrMrX)rrVrrrrMszDictWriter.writerowcCs=g}x$|D]}|j|j|q W|jj|S)N)appendrXr writerows)rZrowdictsZrowsrVrrrrZs zDictWriter.writerowsN)r#r$r%rrOrXrMrZrrrrrs    c@sUeZdZdZddZdddZddZd d Zd d ZdS) rze "Sniffs" the format of a CSV file (i.e. delimiter, quotechar) Returns a Dialect object. cCsdddddg|_dS)Nr-r/; :) preferred)rrrrrszSniffer.__init__NcCs|j||\}}}}|s?|j||\}}n|sTtdnGdddt}||_||_|pd|_||_|S)zI Returns a dialect (or None) corresponding to the sample zCould not determine delimiterc@s"eZdZdZdZeZdS)zSniffer.sniff..dialectZsniffedz N)r#r$r%r&r+r r,rrrrr5s r5r.)_guess_quote_and_delimiter_guess_delimiterrrr)r'r(r*)rsample delimitersr(r)r'r*r5rrrsniffs    z Sniffer.sniffcCs/g}xCdD];}tj|tjtjB}|j|}|r Pq q W|sVdSi}i}d}x|D]} |jd d } | | } | r|j| dd || [^\w "'])(?P ?)(?P["']).*?(?P=quote)(?P=delim)C(?:^| )(?P["']).*?(?P=quote)(?P[^\w "'])(?P ?)D(?P>[^\w "'])(?P ?)(?P["']).*?(?P=quote)(?:$| )*(?:^| )(?P["']).*?(?P=quote)(?:$| )rFNrZquotedelimZspacerDr1z]((%(delim)s)|^)\W*%(quote)s[^%(delim)s\n]*%(quote)s[^%(delim)s\n]*%(quote)s\W*((%(delim)s)|$)T)rdrerfrg)rFNr) recompileDOTALL MULTILINEfindall groupindexrUKeyErrormaxescapesearch)rdatarbZmatchesZrestrZregexpZquotesdelimsZspacesmnrDr(rir*Z dq_regexpr)rrrr_s^         ' z"Sniffer._guess_quote_and_delimitercCsttd|jd}ddtdD}tdt|}d}i}i}i}dt|t|} } x| t|kr)|d7}xn|| | D]\} xS|D]K} |j| i} | j| }| j|dd| |<| || . rrhrDcSs|dS)Nrhr)rTrrrHsz*Sniffer._guess_delimiter..css|]}|dVqdS)rhNr)rPitemrrr Msz+Sniffer._guess_delimiter..g?g?g{Gz?z%c rcSs"g|]\}}||fqSrr)rPrQvrrrrRws )rr)listfiltersplitrangeminrArUcountkeysitemsrqremovesumfloatr^sort)rrtrbasciiZ chunkLengthZ iterationZ charFrequencyZmodesrustartendlinecharZ metaFrequencyZfreqrZmodeListZtotalZ consistencyZ thresholdrQrrir*rCrrrr`st  &$ !   zSniffer._guess_delimiterc Cstt||j|}t|}t|}i}xt|D]}d||s. ^      2!