示例#1
0
 def write( self ):
     CheckAndMakeDirs( self.path )
     filename = self.path + TokensAPI.TOKENS
     with open( filename, 'w' ) as f:
         writer = UnicodeWriter( f )
         for ( docID, docTokens ) in self.data.iteritems():
             writer.writerow( [ docID, ' '.join(docTokens) ] )
示例#2
0
def WriteAsSparseMatrix( matrix, filename ):
	"""
	Expect a sparse matrix (two-level dict) of values.
	Generate a tab-delimited file, with 3 columns.
	Write two keys as the 1st and 2nd columns; write cell value as the 3rd column.
	"""
	sortedKeys = sorted( matrix.keys(), key = lambda key : -matrix[key] )
	with open( filename, 'w' ) as f:
		writer = UnicodeWriter( f )
		for ( aKey, bKey ) in sortedKeys:
			writer.writerow( [ aKey, bKey, str( matrix[ (aKey, bKey) ] ) ] )
示例#3
0
def WriteAsSparseVector( vector, filename ):
	"""
	Expect a sparse vector (dict) of values.
	Generate a tab-delimited file, with 2 columns.
	Write key as the 1st column; write cell value as the 2nd column.
	"""
	sortedKeys = sorted( vector.keys(), key = lambda key : -vector[key] )
	with open( filename, 'w' ) as f:
		writer = UnicodeWriter( f )
		for key in sortedKeys:
			writer.writerow( [ key, str( vector[key] ) ] )
示例#4
0
def WriteAsTabDelimited( data, filename, fields ):
	"""
	Expect a list of dict values.
	Take in a list of output fields.
	Write specified fields to disk, as a tab-delimited file (with header row).
	"""
	with open( filename, 'w' ) as f:
		writer = UnicodeWriter( f )
		writer.writerow( fields )
		for element in data:
			values = []
			for field in fields:
				values.append( str( element[field] ) )
			writer.writerow( values )
示例#5
0
def WriteAsMatrix( matrix, filename ):
	with open( filename, 'w' ) as f:
		writer = UnicodeWriter( f )
		for row in matrix:
			writer.writerow( map( str, row ) )