3 \4?@sLdZddlZddlmZmZmZmZmZmZm Z m Z m Z m Z m Z mZmZmZddlmZddlmZddlmZddd d d d d ddddddddddddddgZGdd d ZGdddeZedeGdddeZedeGd ddeZed!eGd"ddZGd#ddZyeWnek r8eZYnXGd$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) OrderedDict)StringIOr r r rrrrexcel excel_tabr rrrrr Snifferrr DictReader DictWriter unix_dialectc@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|_|jdS)NT) __class__r_valid _validate)selfr/usr/lib64/python3.6/csv.py__init__+s zDialect.__init__cCs@y t|Wn.tk r:}ztt|WYdd}~XnXdS)N)_Dialect TypeErrorrstr)rerrrr0s zDialect._validate)__name__ __module__ __qualname__r_namer delimiter quotecharZ escapechar doublequoteskipinitialspacelineterminatorquotingr rrrrrrsc@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.rrrrr7sc@seZdZdZdZdS)rzEDescribe the usual properties of Excel-generated TAB-delimited files. N)r%r&r'rr)rrrrrAsz excel-tabc@s(eZdZdZdZdZdZdZdZe Z dS)rz:Describe the usual properties of Unix-generated CSV files.r/r0TF N) r%r&r'rr)r*r+r,r-r r.rrrrrFsZunixc@s@eZdZd ddZddZeddZejd dZd d ZdS) rNrcOs6||_||_||_t||f|||_||_d|_dS)Nr) _fieldnamesrestkeyrestvalrdialectline_num)rf fieldnamesr4r5r6argskwdsrrrr Rs zDictReader.__init__cCs|S)Nr)rrrr__iter__[szDictReader.__iter__c Cs@|jdkr0yt|j|_Wntk r.YnX|jj|_|jS)N)r3nextr StopIterationr7)rrrrr9^s  zDictReader.fieldnamescCs ||_dS)N)r3)rvaluerrrr9hscCs|jdkr|jt|j}|jj|_x|gkr:t|j}q&Wtt|j|}t|j}t|}||krz||d||j<n*||krx |j|dD]}|j||<qW|S)Nr) r7r9r=rrziplenr4r5)rrowdZlfZlrkeyrrr__next__ls     zDictReader.__next__)NNNr) r%r&r'r r<propertyr9setterrErrrrrQs   c@s6eZdZdddZddZdd Zd d Zd d ZdS)rrraisercOsB||_||_|jdkr$td|||_t||f|||_dS)NrHignorez-extrasaction (%s) must be 'raise' or 'ignore')rHrI)r9r5lower ValueError extrasactionr)rr8r9r5rLr6r:r;rrrr s zDictWriter.__init__cCs tt|j|j}|j|dS)N)dictr@r9writerow)rheaderrrr writeheaderszDictWriter.writeheadercsNjdkr8jj}|r8tddjdd|DfddjDS)NrHz(dict contains fields not in fieldnames: z, cSsg|] }t|qSr)repr).0xrrr sz,DictWriter._dict_to_list..c3s|]}j|jVqdS)N)getr5)rRrD)rowdictrrr sz+DictWriter._dict_to_list..)rLkeysr9rKjoin)rrVZ wrong_fieldsr)rVrr _dict_to_lists  zDictWriter._dict_to_listcCs|jj|j|S)N)rrNrZ)rrVrrrrNszDictWriter.writerowcCs|jjt|j|S)N)r writerowsmaprZ)rZrowdictsrrrr[szDictWriter.writerowsN)rrHr)r%r&r'r rPrZrNr[rrrrrs  c@s:eZdZdZddZd ddZddZd d Zd d ZdS)rze "Sniffs" the format of a CSV file (i.e. delimiter, quotechar) Returns a Dialect object. cCsdddddg|_dS)Nr/r1; :) preferred)rrrrr szSniffer.__init__NcCsd|j||\}}}}|s(|j||\}}|s4tdGdddt}||_||_|pVd|_||_|S)zI Returns a dialect (or None) corresponding to the sample zCould not determine delimiterc@seZdZdZdZeZdS)zSniffer.sniff..dialectZsniffedz N)r%r&r'r(r-r r.rrrrr6sr6r0)_guess_quote_and_delimiter_guess_delimiterrrr+r)r*r,)rsample delimitersr*r+r)r,r6rrrsniffs  z Sniffer.sniffcCsg}x0dD](}tj|tjtjB}|j|}|r Pq W|s>dSi}i}d}|j} x|D]} | d d } | | } | r|j| dd || <y| d d } | | } Wntk rwVYnX| r|dks| |kr|j| dd || <y| d d } Wntk rwVYnX| | rV|d 7}qVWt||jd } |rZt||jd }|||k}|dkrbd}nd}d}tjdtj || dtj}|j |rd}nd}| |||fS)a 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. I(?P[^\w\n"\'])(?P ?)(?P["\']).*?(?P=quote)(?P=delim)G(?:^|\n)(?P["\']).*?(?P=quote)(?P[^\w\n"\'])(?P ?)G(?P[^\w\n"\'])(?P ?)(?P["\']).*?(?P=quote)(?:$|\n)-(?:^|\n)(?P["\']).*?(?P=quote)(?:$|\n)rFNrquotedelimZspace)rDr2z]((%(delim)s)|^)\W*%(quote)s[^%(delim)s\n]*%(quote)s[^%(delim)s\n]*%(quote)s\W*((%(delim)s)|$))rlrjT)rfrgrhri)rFNr) recompileDOTALL MULTILINEfindall groupindexrUKeyErrormaxescapesearch)rdatardZmatchesZrestrZregexpZquotesdelimsZspacesrrmnrDr*rlr,Z dq_regexpr+rrrras`          z"Sniffer._guess_quote_and_delimitercCsttd|jd}ddtdD}tdt|}d}i}i}i}dt|t|} } x| t|krn|d7}xT|| | D]D} x>|D]6} |j| i} | j| }| j|dd| |<| || <qWqWx|jD]} t|| j }t|dkr|dddkrqt|dkrht |d d d || <|j || || d|| dt d d |Df|| <q|d|| <qW|j }t ||}d}d}xt|dkr||krx\|D]T\}}|ddkr|ddkr|d||kr|dks||kr|||<qW|d8}qWt|dkr`t|jd}|dj||djd|k}||fS| } | |7} q\W|szdSt|dkrxF|jD]<}||jkr|dj||djd|k}||fSqWdd|j D}|j|dd}|dj||djd|k}||fS)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. Nr2cSsg|] }t|qSr)chr)rRcrrrrT.sz,Sniffer._guess_delimiter.. rrkcSs|dS)Nrkr)rSrrrHsz*Sniffer._guess_delimiter..)rDcss|]}|dVqdS)rkNr)rRitemrrrrWMsz+Sniffer._guess_delimiter..g?g?g{Gz?z%c rcSsg|]\}}||fqSrr)rRkvrrrrTws)rr)listfiltersplitrangeminrArUcountrXitemsrtremovesumfloatr`sort)rrwrdasciiZ chunkLengthZ iterationZ charFrequencyZmodesrxstartendlinecharZ metaFrequencyZfreqrZmodeListZtotalZ consistencyZ thresholdrrrlr,rCrrrrbst           zSniffer._guess_delimiterc Cstt||j|}t|}t|}i}xt|D] }d||<q2Wd}x|D]}|dkrZP|d7}t||krpqLxt|jD]x} xJtt t gD]0} y| || PWqt t fk rYqXqWt|| } | || kr~|| dkr| || <q~|| =q~WqLWd} x|j D]~\} } t| tdkrLt|| | krB| d7} n| d8} ns2@       2