Did I find the right examples for you? yes no      Crawl my project      Python Jobs

All Samples(1)  |  Call(1)  |  Derive(0)  |  Import(0)
Creates a matrix instance from a csv representation of a matrix (a string).

Note: Any lines beginning with a hash (#) are ignored.

Parameters
        string: The csv reprensentation
        readHeaders: True if the first (non-comment) line of the string should be interpreted
                as giving the column headers. Default is True.
        dialect: An instance of the csv.Dialect class containing information
        on the dialect of the csv representation. Defaults to None.(more...)

        def matrixFromCSVRepresentation(string, readHeaders=True, dialect=None):

	'''Creates a matrix instance from a csv representation of a matrix (a string).
	
	Note: Any lines beginning with a hash (#) are ignored.
	
	Parameters
		string: The csv reprensentation
		readHeaders: True if the first (non-comment) line of the string should be interpreted
			as giving the column headers. Default is True.
		dialect: An instance of the csv.Dialect class containing information
		on the dialect of the csv representation. Defaults to None.
		In this case the function tries to deduce the dialect itself.
		
	Return
		Returns a Matrix instance
		If the first column header is Mutations returns a PEATSAData matrix
		
	Exceptions:
		Raises a TypeError if a matrix could not be read from the file'''

	#Find the first line that isn't a # into a string.
	#Then use StringIO to treat this string like a file
	#for use with the csv reader object.
	file = StringIO.StringIO(string)
	
	#Seek to the first line that does not begin with a hash
	position = 0
	flag = 0
	while flag == 0:
		line = file.readline()
		if line[0] != '#':
			flag = 1
		else:
			position = file.tell()
	
	file.seek(position)
	fileContent = file.read()
	file.close()
	
	#Create the file-like string object
	csvFile = StringIO.StringIO(fileContent)
	
	#Check the file dialect before processing
	if dialect == None:
		line = csvFile.readline()
		sniffer = csv.Sniffer()
		dialect = sniffer.sniff(line)
		csvFile.seek(0)	
	
	#The reader reads the csv file and converts each
	#line of the file into a list containing the csv elements.
	reader = csv.reader(csvFile, dialect=dialect)
	
	#Read all the rows
	#Remove any elements that are just blanks
	rows = []
	for row in reader:
		row = [el for el in row if el != '']
		rows.append(row)
	
	#Get the column headers if specified
	if readHeaders:
		headers = rows.pop(0)
	else:
		headers = None
		
	csvFile.close()	

	#Convert any floats or ints stored as strings
	convertedRows = []
	for row in rows:
		newRow = []
		for element in row:
			try: 
				#Check if its an int
				#Do this first since all ints can be converted to floats
				element = int(element)
				newRow.append(element)
				continue
			except ValueError:
				pass
				
			try:
				#Check if its a float
				element = float(element)
				newRow.append(element)
				continue
			except ValueError:
				#Its not an int or float
				newRow.append(element)
		
		convertedRows.append(newRow)	
	
	if headers is not None:
		headers = [header.strip() for header in headers]
	
	#If the matrix contains this first columnsreturn a PEATSA matrix
	if headers is not None and 'Mutations' in headers:
		matrix = PEATSAMatrix(convertedRows, headers)
	else:	
		matrix = Matrix(convertedRows, headers)	
					
	return matrix
        


src/p/e/PEATDB-2.3/PEATSA/WebApp/Data.py   PEATDB(Download)
			content = rows[0][1]
			size = rows[0][0]
			matrix = Core.Matrix.matrixFromCSVRepresentation(content)	
 
		return matrix