a
    WJC                     @   s.   d dl Z d dlZdd Zdd Zd	ddZdS )
    Nc           	      C   sb  | dkrt j}n<zt| d}W n,   tt j? d|  d f t d Y n0 g }d}d}| }|dkrlqL|d7 }|d d	kr|dd }td
d|}tdd|}tdd|}t	d|rqX|
 }t|}|dkr|}td|D ]}|g  qn*||kr&tt j? d| |f f t d td|D ]}|| ||  q0qX| dkr^|  |S )a9  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']].
	-rCouldn't open "z" for read.   r    
z#.*z^\s+z\s+$z^$z%Ragged input in file "%s" at line %d.)sysstdinopenprintstderrexitreadlinereZsubZmatchsplitlenrangeappendclose)		file_namefile_handlecolumnsZ
num_fieldsZline_numberlineZfieldsZcurr_num_fieldsj r   L/homepages/25/d321765456/htdocs/pub_http_internet/doc/python-talk/tabutil.pytext_columns_from_file   sJ    



r   c                 C   sX   t | }t|}t|d }td|D ],}td|D ]}t|| | || |< q4q&|S )a(  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]]
	r   )r   r   r   float)r   r   num_rowsZnum_colsir   r   r   r   float_columns_from_fileS   s    r!   %11.7fc                 C   s   |dkrt j}n<zt|d}W n,   tt j? d| d f t d Y n0 t| }t| d }td|D ]8}td|D ]}t|? || | |  f qxt|? df qj|dkr|  dS )	zGiven 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   wr   z" for write.r   r   r   N)	r	   stdoutr   r   r   r   r   r   r   )r   r   formatr   Znum_columnsr   r    r   r   r   r   float_columns_to_filej   s"    
r&   )r"   )r	   r   r   r!   r&   r   r   r   r   <module>   s   D