Your IP : 3.148.178.125
o
S�g�>�@sdZddlZddlmZmZmZmZmZmZm Z m
Z
mZmZm
Z
mZmZmZddlmZddlmZgd�ZGdd�d�ZGd d
�d
e�Zed
e�Gdd�de�Zed
e�Gdd�de�Zede�Gdd�d�ZGdd�d�ZzeWney�eZYnwGdd�d�ZdS)z+
csv.py - read/write/investigate CSV files
�N)�Error�__version__�writer�reader�register_dialect�unregister_dialect�get_dialect�
list_dialects�field_size_limit�
QUOTE_MINIMAL� QUOTE_ALL�QUOTE_NONNUMERIC�
QUOTE_NONE�__doc__)�Dialect)�StringIO)rrr
rrrr�excel� excel_tabr
rrrrr �Snifferrr�
DictReader�
DictWriter�unix_dialectc@sDeZdZdZdZdZdZdZdZdZ dZ
dZdZdd�Z
dd�ZdS) rz�Describe a CSV dialect.
This must be subclassed (see csv.excel). Valid attributes are:
delimiter, quotechar, escapechar, doublequote, skipinitialspace,
lineterminator, quoting.
�FNcCs|jtkrd|_|��dS)NT)� __class__r�_valid� _validate��self�r�*/opt/alt/python310/lib64/python3.10/csv.py�__init__*s
zDialect.__init__c
Cs4zt|�WdSty}ztt|���d}~ww�N)�_Dialect� TypeErrorr�str)r�errrr/s��zDialect._validate)�__name__�
__module__�__qualname__r�_namer� delimiter� quotecharZ
escapechar�doublequote�skipinitialspace�lineterminator�quotingr rrrrrrsrc@�(eZdZdZdZdZdZdZdZe Z
dS)rz;Describe the usual properties of Excel-generated CSV files.�,�"TF�
N)r&r'r(rr*r+r,r-r.rr/rrrrr6�rc@seZdZdZdZdS)rzEDescribe the usual properties of Excel-generated TAB-delimited files.� N)r&r'r(rr*rrrrr@srz excel-tabc@r0)rz:Describe the usual properties of Unix-generated CSV files.r1r2TF�
N)r&r'r(rr*r+r,r-r.rr/rrrrrEr4rZunixc@sDeZdZ ddd�Zdd�Zedd��Zejd d��Zd
d�ZdS)
rNrcOs<||_||_||_t||g|�Ri|��|_||_d|_dS�Nr)�_fieldnames�restkey�restvalr�dialect�line_num)r�f�
fieldnamesr9r:r;�args�kwdsrrrr Qs
zDictReader.__init__cCs|Sr!rrrrr�__iter__ZszDictReader.__iter__cCs>|jdurzt|j�|_Wn tyYnw|jj|_|jSr!)r8�nextr�
StopIterationr<rrrrr>]s
�
zDictReader.fieldnamescCs
||_dSr!)r8)r�valuerrrr>gs
cCs�|jdkr|jt|j�}|jj|_|gkrt|j�}|gkstt|j|��}t|j�}t|�}||kr?||d�||j<|S||krR|j|d�D]}|j||<qJ|Sr7) r<r>rBr�dict�zip�lenr9r:)r�row�dZlfZlr�keyrrr�__next__ks"
�
�zDictReader.__next__)NNNr) r&r'r(r rA�propertyr>�setterrKrrrrrPs
�
rc@s:eZdZ ddd�Zdd�Zdd �Zd
d�Zdd
�ZdS)rr�raisercOsH||_||_|��dvrtd|��||_t||g|�Ri|��|_dS)N)rN�ignorez-extrasaction (%s) must be 'raise' or 'ignore')r>r:�lower�
ValueError�extrasactionr)rr=r>r:rRr;r?r@rrrr �s�zDictWriter.__init__cCstt|j|j��}|�|�Sr!)rErFr>�writerow)r�headerrrr�writeheader�s
zDictWriter.writeheadercsN�jdkr����j}|rtdd�dd�|D������fdd��jD�S)NrNz(dict contains fields not in fieldnames: z, cS�g|]}t|��qSr)�repr)�.0�xrrr�
<listcomp>��z,DictWriter._dict_to_list.<locals>.<listcomp>c3s�|]
}��|�j�VqdSr!)�getr:)rXrJ��rowdictrrr� <genexpr>�s�z+DictWriter._dict_to_list.<locals>.<genexpr>)rR�keysr>rQ�join)rr^Zwrong_fieldsrr]r�
_dict_to_list�s
�zDictWriter._dict_to_listcCs|j�|�|��Sr!)rrSrb)rr^rrrrS�szDictWriter.writerowcCs|j�t|j|��Sr!)r� writerows�maprb)rZrowdictsrrrrc�szDictWriter.writerowsN)rrNr)r&r'r(r rUrbrSrcrrrrr�s
�
rc@s:eZdZdZdd�Zd
dd�Zdd�Zd d
�Zdd�ZdS)rze
"Sniffs" the format of a CSV file (i.e. delimiter, quotechar)
Returns a Dialect object.
cCsgd�|_dS)N)r1r5�;� �:)� preferredrrrrr �szSniffer.__init__NcCsd|�||�\}}}}|s|�||�\}}|std��Gdd�dt�}||_||_|p+d|_||_|S)zI
Returns a dialect (or None) corresponding to the sample
zCould not determine delimiterc@seZdZdZdZeZdS)zSniffer.sniff.<locals>.dialectZsniffedr3N)r&r'r(r)r.rr/rrrrr;�sr;r2)�_guess_quote_and_delimiter�_guess_delimiterrrr,r*r+r-)r�sample�
delimitersr+r,r*r-r;rrr�sniff�s
��
z
Sniffer.sniffc Cs�g}dD]}t�|tjtjB�}|�|�}|rnq|sdSi}i}d}|j} |D]\}
| dd}|
|}|rA|�|d�d||<z| dd}|
|}Wn tyVYq)w|rk|dusa||vrk|�|d�d||<z| dd}Wn ty|Yq)w|
|r�|d7}q)t||jd �}
|r�t||jd �}|||k}|d
kr�d}nd}d}t�dt� |�|
d
�tj�}|�
|�r�d}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.
)zI(?P<delim>[^\w\n"\'])(?P<space> ?)(?P<quote>["\']).*?(?P=quote)(?P=delim)zG(?:^|\n)(?P<quote>["\']).*?(?P=quote)(?P<delim>[^\w\n"\'])(?P<space> ?)zG(?P<delim>[^\w\n"\'])(?P<space> ?)(?P<quote>["\']).*?(?P=quote)(?:$|\n)z-(?:^|\n)(?P<quote>["\']).*?(?P=quote)(?:$|\n))rFNrr�quote��delimNZspace�rJr6rz]((%(delim)s)|^)\W*%(quote)s[^%(delim)s\n]*%(quote)s[^%(delim)s\n]*%(quote)s\W*((%(delim)s)|$))rprnTF)�re�compile�DOTALL� MULTILINE�findall�
groupindexr\�KeyError�max�escape�search)r�datarlZmatchesZrestrZregexpZquotes�delims�spacesrw�m�nrJr+rpr-Z dq_regexpr,rrrri�sj
�������
z"Sniffer._guess_quote_and_delimitercCsttd|�d���}dd�td�D�}tdt|��}d}i}i}i}d|} }
| t|�k�r-|d7}|| |
�D] }|D]}|�|i�}
|�|�}|
�|d�d|
|<|
||<q<q8|��D]N}t||� ��}t|�dkrv|dddkrvq]t|�dkr�t
|d d
�d�||<|�||�||d||dtdd
�|D��f||<q]|d||<q]|� �}t
t||t|���}d}d}t|�dkr�||kr�|D]$\}}|ddkr�|ddkr�|d||kr�|dus�||vr�|||<q�|d8}t|�dkr�||ks�t|�dk�r!t|���d}|d�|�|d�d|�k}||fS|
} |
|7}
| t|�ks.|�s2dSt|�dk�r]|jD] }||��v�r[|d�|�|d�d|�k}||fS�q<dd�|� �D�}|��|dd}|d�|�|d�d|�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.
Nr6cSrVr)�chr)rX�crrrrZ-r[z,Sniffer._guess_delimiter.<locals>.<listcomp>��
rrocSs|dS)Nror)rYrrr�<lambda>Gsz*Sniffer._guess_delimiter.<locals>.<lambda>rqcss�|]}|dVqdS)roNr)rX�itemrrrr_Ls�z+Sniffer._guess_delimiter.<locals>.<genexpr>g�?g�������?g{�G�z�?z%c )rrcSsg|]\}}||f�qSrr)rX�k�vrrrrZvs���)�list�filter�split�range�minrGr\�countr`�itemsry�remove�sum�floatrh�sort)rr|rl�asciiZchunkLengthZ iterationZ
charFrequencyZmodesr}�start�end�line�charZ
metaFrequencyZfreqr�ZmodeList�totalZconsistencyZ thresholdr�r�rpr-rIrrrrjs�
������3
���zSniffer._guess_delimiterc
Csjtt|�|�|��}t|�}t|�}i}t|�D]}d||<qd}|D]L}|dkr+nE|d7}t|�|kr6q#t|���D]2} t}
z|
|| �Wnt t
fyYt|| �}
Ynw|
|| krn|| durk|
|| <q<|| =q<q#d}|��D]:\} }t|�td�kr�t|| �|kr�|d7}qv|d8}qvz||| �Wnt t
fy�|d7}Yqvw|d8}qv|dkS)Nr�ro)rrrmrBrGr�r�r`�complexrQ�
OverflowErrorr��typer#)
rrkZrdrrT�columnsZcolumnTypes�i�checkedrH�colZthisTypeZ hasHeaderZcolTyperrr�
has_headersL
�
��
�
zSniffer.has_headerr!) r&r'r(rr rmrirjr�rrrrr�s
Lgr)rrrZ_csvrrrrrrrr r
rrr
rrr"�ior�__all__rrrrrr�� NameErrorr�rrrrr�<module>s*@
2�
?>