Example #1
0
def readCplxData(filename, commentSymbol="#"):
    """
        Read a data file and return a nested list (data block).
        Each line contains data from each row (sub-list).
        All lines containing the commentSymbol are ignored.
        Only numerical data are read.
        This is the complex version. It is assumed that there
        are even number of columns and the first half of them
        store the real part of the variables and the second
        half store the imaginary part.
    """
    inFile = open(filename, "r")
    data = []
    for aLine in inFile.readlines():
        if aLine.find(commentSymbol)!=-1: continue
        lineData = []
        splited = aLine.split(); # split the line

        # usual read & convert
        for piece in splited:
            if listR.isFloat(piece): # is numerical
                lineData.append(float(piece))

        # skip empty ones
        if not lineData: continue

        # fold columns to form complex data
        number_of_columns = len(lineData)
        if number_of_columns % 2 !=0:
            print("fileRVer2.readCplxData Error: the number of columns in %s must be even!" % filename)
            exit()
        half_columns = int(number_of_columns/2)
        data.append([lineData[col]+1j*lineData[col+half_columns] for col in range(half_columns)]); # add only non-empty lines
    inFile.close()
    return data
Example #2
0
def strStream2BlockStream(
    dataStream, numericalOnly=False, separators=[",", " ", ";"], commentSymbol="#", skipEmpty=True
):
    """
        Read a string data stream and for each time it is called, yield a list
        corresponding to data converted from the corresponding string.
        -- dataStream: anything that when using the iterator interface
            gives a string.
        -- numericalOnly: when set to true, only numerical data will be
            included in the return block.
        -- separators: used to break string into substrings.
        -- commentSymbol: anything after this symbol in the string read
            from the dataStream will be discarded.
        -- skipEmpty: whether empty lines are skipped (not yielded). A line
            is empty if the final result is an empty list. Note that a non-empty
            string may give an empty list because of the numericalOnly parameter.
    """
    # loop over the stream
    for aLine in dataStream:
        # deal with the read line, append it to the block
        commentSymbolLocation = aLine.find(commentSymbol)
        if commentSymbolLocation != -1:
            aLine = aLine[:commentSymbolLocation]  # contains the comment symbol
        lineData = []  # store the converted results
        splited = separateStr(aLine, separators)
        # split the line
        for piece in splited:
            if isFloat(piece):  # is numerical
                lineData.append(float(piece))
            else:  # is a string
                if not numericalOnly:
                    lineData.append(piece)
        if lineData:  # check if it's empty
            yield lineData
def strStream2BlockStream(dataStream, numericalOnly=False, separators=[",", " ", ";"], commentSymbol="#", skipEmpty=True):
    """
        Read a string data stream and for each time it is called, yield a list
        corresponding to data converted from the corresponding string.
        -- dataStream: anything that when using the iterator interface
            gives a string.
        -- numericalOnly: when set to true, only numerical data will be
            included in the return block.
        -- separators: used to break string into substrings.
        -- commentSymbol: anything after this symbol in the string read
            from the dataStream will be discarded.
        -- skipEmpty: whether empty lines are skipped (not yielded). A line
            is empty if the final result is an empty list. Note that a non-empty
            string may give an empty list because of the numericalOnly parameter.
    """
    # loop over the stream
    for aLine in dataStream:
        # deal with the read line, append it to the block
        commentSymbolLocation = aLine.find(commentSymbol)
        if commentSymbolLocation != -1: aLine = aLine[:commentSymbolLocation] # contains the comment symbol
        lineData = [] # store the converted results
        splited = separateStr(aLine, separators); # split the line
        for piece in splited:
            if isFloat(piece): # is numerical
                lineData.append(float(piece))
            else: # is a string
                if not numericalOnly: lineData.append(piece)
        if lineData: # check if it's empty
            yield lineData
Example #4
0
def readNumericalDataI(filename, commentSymbol="#"):
    """
        Read a data file and return a nested list (data block).
        Each line contains data from each row (sub-list).
        All lines containing the commentSymbol (the whole lines)
        are ignored. Only numerical data are read.
        This is the iterator version.
    """
    inFile = open(filename, "r")
    data = []
    for aLine in inFile.readlines():
        if aLine.find(commentSymbol)!=-1: continue
        lineData = []
        splited = aLine.split(); # split the line
        for piece in splited:
            if listR.isFloat(piece): # is numerical
                lineData.append(float(piece))
        if lineData: yield lineData; # yield only non-empty lines
    inFile.close()
Example #5
0
def readData(filename, commentSymbol="#"):
    """
		Read a data file and return a nested list (data block).
		Each line contains data from each row (sub-list).
		All lines contain the commentSymbol are ignored.
	"""
    inFile = open(filename, "r")
    data = []
    for aLine in inFile.readlines():
        if aLine.find(commentSymbol) != -1: continue
        lineData = []
        for piece in aLine.split():
            if listR.isFloat(piece):  # is numerical
                lineData.append(float(piece))
            else:  # is a string
                lineData.append(piece)
        data.append(lineData)
    inFile.close()
    return data
Example #6
0
def readData(filename, commentSymbol="#"):
	"""
		Read a data file and return a nested list (data block).
		Each line contains data from each row (sub-list).
		All lines contain the commentSymbol are ignored.
	"""
	inFile = open(filename, "r")
	data = []
	for aLine in inFile.readlines():
		if aLine.find(commentSymbol)!=-1: continue
		lineData = []
		for piece in aLine.split():
			if listR.isFloat(piece): # is numerical
				lineData.append(float(piece))
			else: # is a string
				lineData.append(piece)
		data.append(lineData)
	inFile.close()
	return data
Example #7
0
def assignmentExprStream2Dict(data, assignmentSymbol="=", commentSymbol="#"):
    """ Assume that each line in the "data" stream is an assignment
    expression (e.g. "x=1", "x:1", etc), return a dictionary representing
    such assginments. The keys are the striped LHS of the assignments
    and the values are the RHS assignments, converted into numerics
    if possible.
    Any line without the assignmentSymbol will be ignored.
    Any string beyond the commentSymbol is discarded.
    The returned value is a dictionary, whose keys and values are strings.
    """
    return_dict = {};
    for aLine in data:
        assignmentSymbolLocation = aLine.find(assignmentSymbol)
        if assignmentSymbolLocation == -1: continue # no assignmentSymbol found in the line
        commentSymbolLocation = aLine.find(commentSymbol)
        if commentSymbolLocation != -1: aLine_noComment = aLine[:commentSymbolLocation] # contains the comment symbol
        LHS = aLine[:assignmentSymbolLocation].strip()
        RHS = aLine[assignmentSymbolLocation+1:].strip()
        if isFloat(RHS): RHS = float(RHS)
        return_dict[LHS] = RHS
    return return_dict
def assignmentExprStream2Dict(data, assignmentSymbol="=", commentSymbol="#"):
    """ Assume that each line in the "data" stream is an assignment
    expression (e.g. "x=1", "x:1", etc), return a dictionary representing
    such assginments. The keys are the striped LHS of the assignments
    and the values are the RHS assignments, converted into numerics
    if possible.
    Any line without the assignmentSymbol will be ignored.
    Any string beyond the commentSymbol is discarded.
    The returned value is a dictionary, whose keys and values are strings.
    """
    return_dict = {}
    for aLine in data:
        assignmentSymbolLocation = aLine.find(assignmentSymbol)
        if assignmentSymbolLocation == -1:
            continue  # no assignmentSymbol found in the line
        commentSymbolLocation = aLine.find(commentSymbol)
        if commentSymbolLocation != -1:
            aLine_noComment = aLine[:
                                    commentSymbolLocation]  # contains the comment symbol
        LHS = aLine[:assignmentSymbolLocation].strip()
        RHS = aLine[assignmentSymbolLocation + 1:].strip()
        if isFloat(RHS): RHS = float(RHS)
        return_dict[LHS] = RHS
    return return_dict