Dfc@sdZddlZddlmZddlmZmZmZmZm Z m Z m Z m Z m Z mZmZmZmZmZddlmZyddlmZWn!ek rddlmZnXddd d d d d dddddddddddddgZd d"dYZdefdYZe dedefdYZe dedd#dYZdd$d YZyeWnek reZnXdd%d!YZdS(&s+ csv.py - read/write/investigate CSV files iN(treduce(tErrort __version__twritertreadertregister_dialecttunregister_dialectt get_dialectt list_dialectstfield_size_limitt QUOTE_MINIMALt QUOTE_ALLtQUOTE_NONNUMERICt QUOTE_NONEt__doc__(tDialect(tStringIOR R R R RRRtexcelt excel_tabR RRRRRtSnifferRRt DictReadert DictWritercBsVeZdZdZeZdZdZdZ dZ dZ dZ dZ dZdZRS(sDescribe an Excel dialect. This must be subclassed (see csv.excel). Valid attributes are: delimiter, quotechar, escapechar, doublequote, skipinitialspace, lineterminator, quoting. tcCs)|jtkrt|_n|jdS(N(t __class__RtTruet_validt _validate(tself((s/usr/lib64/python2.7/csv.pyt__init__-s cCs:yt|Wn%tk r5}tt|nXdS(N(t_Dialectt TypeErrorRtstr(Rte((s/usr/lib64/python2.7/csv.pyR2sN(t__name__t __module__Rt_nametFalseRtNonet delimitert quotechart escapechart doublequotetskipinitialspacetlineterminatortquotingRR(((s/usr/lib64/python2.7/csv.pyRs cBs2eZdZdZdZeZeZdZ e Z RS(s;Describe the usual properties of Excel-generated CSV files.t,t"s ( R!R"RR&R'RR)R$R*R+R R,(((s/usr/lib64/python2.7/csv.pyR9scBseZdZdZRS(sEDescribe the usual properties of Excel-generated TAB-delimited files.s (R!R"RR&(((s/usr/lib64/python2.7/csv.pyRCss excel-tabcBsPeZdddddZdZedZejdZdZRS(RcOsI||_||_||_t|||||_||_d|_dS(Ni(t _fieldnamestrestkeytrestvalRtdialecttline_num(Rtft fieldnamesR0R1R2targstkwds((s/usr/lib64/python2.7/csv.pyRJs     cCs|S(N((R((s/usr/lib64/python2.7/csv.pyt__iter__SscCsR|jdkr<y|jj|_Wq<tk r8q<Xn|jj|_|jS(N(R/R%Rtnextt StopIterationR3(R((s/usr/lib64/python2.7/csv.pyR5Vs cCs ||_dS(N(R/(Rtvalue((s/usr/lib64/python2.7/csv.pyR5`scCs|jdkr|jn|jj}|jj|_x|gkrX|jj}q:Wtt|j|}t|j}t|}||kr||||jeZddddZdZdZdZdZRS(RtraiseRcOsY||_||_|jdkr4td|n||_t|||||_dS(NRFtignores-extrasaction (%s) must be 'raise' or 'ignore'(RFsignore(R5R1tlowert ValueErrort extrasactionR(RR4R5R1RJR2R6R7((s/usr/lib64/python2.7/csv.pyR|s    cCs,tt|j|j}|j|dS(N(R<R=R5twriterow(Rtheader((s/usr/lib64/python2.7/csv.pyt writeheaderscCs|jdkr\g|D]}||jkr|^q}|r\tddj|q\ng|jD]}|j||j^qfS(NRFs(dict contains fields not in fieldnames: s, (RJR5RItjointgetR1(Rtrowdicttkt wrong_fieldsRC((s/usr/lib64/python2.7/csv.pyt _dict_to_lists (cCs|jj|j|S(N(RRKRS(RRP((s/usr/lib64/python2.7/csv.pyRKscCs=g}x$|D]}|j|j|q W|jj|S(N(tappendRSRt writerows(RtrowdictstrowsRP((s/usr/lib64/python2.7/csv.pyRUs (R!R"RRMRSRKRU(((s/usr/lib64/python2.7/csv.pyR{s    cBs>eZdZdZddZdZdZdZRS(se "Sniffs" the format of a CSV file (i.e. delimiter, quotechar) Returns a Dialect object. cCsdddddg|_dS(NR-s t;t t:(t preferred(R((s/usr/lib64/python2.7/csv.pyRscCs|j||\}}}}|s?|j||\}}n|sQtdndtfdY}||_||_|pd|_||_|S(sI Returns a dialect (or None) corresponding to the sample sCould not determine delimiterR2cBseZdZdZeZRS(tsniffeds (R!R"R#R+R R,(((s/usr/lib64/python2.7/csv.pyR2sR.(t_guess_quote_and_delimitert_guess_delimiterRRR)R&R'R*(Rtsamplet delimitersR'R)R&R*R2((s/usr/lib64/python2.7/csv.pytsniffs     cCs<g}xCdD];}tj|tjtjB}|j|}|r Pq q W|sbdtddfSi}i}d}x|D]} |jdd} | | } | r|j| dd|| [^\w "'])(?P ?)(?P["']).*?(?P=quote)(?P=delim)sC(?:^| )(?P["']).*?(?P=quote)(?P[^\w "'])(?P ?)sD(?P>[^\w "'])(?P ?)(?P["']).*?(?P=quote)(?:$| )s*(?:^| )(?P["']).*?(?P=quote)(?:$| )RitquoteitdelimtspacecSs||||kr|p|S(N((tatbtquotes((s/usr/lib64/python2.7/csv.pytscSs||||kr|p|S(N((ReRftdelims((s/usr/lib64/python2.7/csv.pyRhss s]((%(delim)s)|^)\W*%(quote)s[^%(delim)s\n]*%(quote)s[^%(delim)s\n]*%(quote)s\W*((%(delim)s)|$)(sF(?P[^\w "'])(?P ?)(?P["']).*?(?P=quote)(?P=delim)sC(?:^| )(?P["']).*?(?P=quote)(?P[^\w "'])(?P ?)sD(?P>[^\w "'])(?P ?)(?P["']).*?(?P=quote)(?:$| )s*(?:^| )(?P["']).*?(?P=quote)(?:$| )N(tretcompiletDOTALLt MULTILINEtfindallR$R%t groupindexROtKeyErrorRtkeystsearchR(RtdataR`tmatchestrestrtregexpRgRitspacestmtnRCR'RcR*t dq_regexpR)((s/usr/lib64/python2.7/csv.pyR]s`             cCstd|jd}gtdD]}t|^q%}tdt|}d}i}i}i} dt|t|} } x| t|kr|d7}xk|| | !D]\} xS|D]K} |j| i}| j| }|j|dd||<||| |dj||djd |k}||fSq>Wng| j D]\}}||f^q}|j|d d}|dj||djd |k}||fS(s 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. s ii iicSs|d|dkr|p|S(Ni((ReRf((s/usr/lib64/python2.7/csv.pyRhDscSsd|d|dfS(Nii((ReRf((s/usr/lib64/python2.7/csv.pyRhJsg?g?g{Gz?s%c RiN(Ri(tfilterR%tsplittrangetchrtminR>ROtcountRqtitemsRtremovetfloatR[tsort(RRsR`tctasciit chunkLengtht iterationt charFrequencytmodesRitstarttendtlinetchart metaFrequencytfreqRtmodeListttotalt consistencyt thresholdRQtvRcR*R@((s/usr/lib64/python2.7/csv.pyR^sx%  &    !  + c Cstt||j|}|j}t|}i}xt|D]}d||R}R%RqtinttlongRtcomplexRIt OverflowErrorRttypeR( RR_trdrRLtcolumnst columnTypestitcheckedR?tcoltthisTypet hasHeadertcolType((s/usr/lib64/python2.7/csv.pyt has_header~sN           N( R!R"RRR%RaR]R^R(((s/usr/lib64/python2.7/csv.pyRs    L i((((( RRjt functoolsRt_csvRRRRRRRRR R R R R RRt cStringIORt ImportErrort__all__RRRRRt NameErrorRR(((s/usr/lib64/python2.7/csv.pyts2 ^      2"