U
    ]b                     @   s   d dl Z dddZdS )    NT,Fc                    s  |r|st dtj| |d}|r*t|ng  |rH fdd|D }| g }t|dD ]\}	sdqV|rzfdd|D |rzdd t|D W nV tk
r }
 z8|std|	 d	  td|	 d
|
  W Y qVW 5 d}
~
X Y nX  rtt }nt	}|
| qV|S )zG
    Parse a CSV file into a list of records with type conversion.
    zselect requires column headers)	delimiterc                    s   g | ]}  |qS  )index).0Zcolname)headersr   6/home/cheung/ML/practical-python/work/4.1/fileparse.py
<listcomp>   s     zparse_csv.<locals>.<listcomp>   c                    s   g | ]} | qS r   r   )r   r   )rowr   r   r	      s     c                 S   s   g | ]\}}||qS r   r   )r   funcvalr   r   r   r	   !   s     zRow z: Couldn't convert z	: Reason N)RuntimeErrorcsvreadernext	enumeratezip
ValueErrorprintdicttupleappend)linesZselecttypesZhas_headersr   Zsilence_errorsZrowsindicesZrecordsZrownoerecordr   )r   r   r   	parse_csv   s4    r   )NNTr   F)r   r   r   r   r   r   <module>   s   