ó
WÎJc           @   s:   d  d l  Z  d  d l Z d   Z d   Z d d  Z d S(   i˙˙˙˙Nc   	      C   sß  |  d k r t  j } n? y t |  d  } Wn) t  j d |  d IJt  j d  n Xg  } d } d } xV| j   } | d k r Pn  | d 7} | d d	 k r˛ | d d !} n  t j d
 d |  } t j d d |  } t j d d |  } t j d |  r	ql n  | j	   } t
 |  } | d k r]| } xZ t d |  D] } | j g   qCWn3 | | k rt  j d |  | f IJt  j d  n  x, t d |  D] } | | j | |  q Wql |  d k rŰ| j   n  | S(   s9  Given an input file containing tabular data, returns a list of columns.
	Pound signs may be used as comments; blank lines are skipped.
	For example, if the input file is

	1 2 3
	4 5 6 # Comment here

	7 8 9
	a b c

	then this routine returns
	[['1', '4', '7', 'a'], ['2', '5', '8', 'b'], ['3', '6', '9', 'c']].
	t   -t   rs   Couldn't open "s   " for read.i   i˙˙˙˙i    t    s   
s   #.*s   ^\s+s   \s+$s   ^$s%   Ragged input in file "%s" at line %d.(   t   syst   stdint   opent   stderrt   exitt   readlinet   ret   subt   matcht   splitt   lent   ranget   appendt   close(	   t	   file_namet   file_handlet   columnst
   num_fieldst   line_numbert   linet   fieldst   curr_num_fieldst   j(    (    sL   /homepages/25/d321765456/htdocs/pub_http_internet/doc/python-talk/tabutil.pyt   text_columns_from_file   sJ    

c         C   s|   t  |   } t |  } t | d  } xM t d |  D]< } x3 t d |  D]" } t | | |  | | | <qN Wq8 W| S(   s(  Given an input file containing float tabular data, returns a list of columns.
	Pound signs may be used as comments; blank lines are skipped.
	For example, if the input file is

	1 2 3
	4 5 6 # Comment here

	7 8 9

	then this routine returns
	[[1.0, 4.0, 7.0], [2.0, 5.0, 8.0], [3.0, 6.0, 9.0]]
	i    (   R   R   R   t   float(   R   R   t   num_rowst   num_colst   iR   (    (    sL   /homepages/25/d321765456/htdocs/pub_http_internet/doc/python-talk/tabutil.pyt   float_columns_from_fileS   s    $s   %11.7fc         C   sä   | d k r t  j } n? y t | d  } Wn) t  j d | d IJt  j d  n Xt |   } t |  d  } xQ t d |  D]@ } x- t d |  D] } | | |  | | Iq W| d IJq W| d k rŕ | j   n  d S(	   sń   Given a list of columns, write the data to a table file.
	For example, if the input is

	[[1.0, 4.0, 7.0], [2.0, 5.0, 8.0], [3.0, 6.0, 9.0]]

	then the output file contains

	1.0000 2.0000 3.0000
	4.0000 5.0000 6.0000
	7.0000 8.0000 9.0000
	R    t   ws   Couldn't open "s   " for write.i   i    R   N(   R   t   stdoutR   R   R   R   R   R   (   R   R   t   formatR   t   num_columnsR   R   R   (    (    sL   /homepages/25/d321765456/htdocs/pub_http_internet/doc/python-talk/tabutil.pyt   float_columns_to_filej   s     (   R   R	   R   R   R$   (    (    (    sL   /homepages/25/d321765456/htdocs/pub_http_internet/doc/python-talk/tabutil.pyt   <module>   s   	D	