read_ftsv and write_ftsv now supports filenames as well as file descriptors.
This commit is contained in:
parent
04126e9c83
commit
e08dba0924
|
@ -444,6 +444,11 @@ def write_ftsv(fd, ds):
|
||||||
@param ds: The dataset to be written. The function handles datasets
|
@param ds: The dataset to be written. The function handles datasets
|
||||||
of these classes: Dataset, CategoryDataset and GraphDataset
|
of these classes: Dataset, CategoryDataset and GraphDataset
|
||||||
"""
|
"""
|
||||||
|
opened = False
|
||||||
|
if isinstance(fd, str):
|
||||||
|
fd = open(fd, 'w')
|
||||||
|
opened = True
|
||||||
|
|
||||||
# Write header information
|
# Write header information
|
||||||
if isinstance(ds, CategoryDataset):
|
if isinstance(ds, CategoryDataset):
|
||||||
type = 'category'
|
type = 'category'
|
||||||
|
@ -475,6 +480,8 @@ def write_ftsv(fd, ds):
|
||||||
print >> fd, "%s\t" % m[j, i],
|
print >> fd, "%s\t" % m[j, i],
|
||||||
print >> fd
|
print >> fd
|
||||||
|
|
||||||
|
if opened:
|
||||||
|
fd.close()
|
||||||
|
|
||||||
def read_ftsv(fd):
|
def read_ftsv(fd):
|
||||||
"""Read a dataset in fluents tab separated values (ftsv) form and return it.
|
"""Read a dataset in fluents tab separated values (ftsv) form and return it.
|
||||||
|
@ -483,6 +490,11 @@ def read_ftsv(fd):
|
||||||
@return: A Dataset, CategoryDataset or GraphDataset depending on the information
|
@return: A Dataset, CategoryDataset or GraphDataset depending on the information
|
||||||
read.
|
read.
|
||||||
"""
|
"""
|
||||||
|
opened = False
|
||||||
|
if isinstance(fd, str):
|
||||||
|
fd = open(fd)
|
||||||
|
opened = True
|
||||||
|
|
||||||
split_re = re.compile('^#\s*(\w+)\s*:\s*(.+)')
|
split_re = re.compile('^#\s*(\w+)\s*:\s*(.+)')
|
||||||
dimensions = []
|
dimensions = []
|
||||||
identifiers = {}
|
identifiers = {}
|
||||||
|
@ -548,5 +560,9 @@ def read_ftsv(fd):
|
||||||
else:
|
else:
|
||||||
ds = Dataset(matrix, dims, name)
|
ds = Dataset(matrix, dims, name)
|
||||||
|
|
||||||
|
if opened:
|
||||||
|
fd.close()
|
||||||
|
|
||||||
return ds
|
return ds
|
||||||
|
|
||||||
|
|
||||||
|
|
Reference in New Issue