Hamburger Hamburger

python Library

writeDictOfArrays

Keine Erläuterungen gefunden.

#==================================================# 
def  writeDictOfArrays(data,fileName):
    """
    save data to a file, ordered in columns, 
    where the first row holds the data names
    """
    fp = open(fileName, 'w', encoding='utf-8')
    keys = data.keys()
    NoKeys = len(keys)
    firstKey = list(keys)[0]
    NoLines = len(data[firstKey])
    for  k,key in enumerate(keys):
        if k==NoKeys-1:
            fp.write(str(key)+"\n")
        else:
            fp.write(str(key)+"\t")
    for  l in range(NoLines):
        for  k,key in enumerate(keys):
            if isinstance(data[key][l],str):
                fp.write(data[key][l])
            else:
                fp.write('{0:1.2E}'.format(data[key][l]))
            if k==NoKeys-1:
                fp.write("\n")
            else:
                fp.write("\t")
                
    fp.close()
    print("Find the data in: "+fileName)
    
#==================================================#
def  writeDictOfArrays2mat(data,fileName):
    """
    https://docs.scipy.org/doc/scipy/reference/generated/scipy.io.savemat.html
    """
    from scipy.io import  savemat
    
    keys = data.keys()
    #NoKeys = len(keys)
    firstKey = list(keys)[0]
    NoLines = len(data[firstKey])
    matData = {}
    for  k in keys:
        mk = k
        mk = mk.replace("+","_")
        mk = mk.replace("-","_")
        mk = mk.replace("*","_")
        mk = mk.replace("/","_")
        matData[mk] = data[k]
    
    savemat(fileName, matData, appendmat=True, format='5', long_field_names=False, do_compression=False, oned_as='column')
    
    print("Find the data in: "+fileName)


    
#=======================================#
if __name__ == "__main__":
    import   numpy as   np
    
    data = {"x":[], "y":[], "z":[]}
    for  key in list(data.keys()):
        data[key] = np.random.uniform(low=0.0, high=10.0, size=10)

    fileName = "foo.dat"
    writeDictOfArrays(data,fileName)
    fileName = "foo.mat"
    writeDictOfArrays2mat(data,fileName)

Index of Library

1ArnesKonto.py
2CIEcolorCoordinates_Spectrum.py
3MatlabStructures.py
4ModelicaExecution.py
5ModelicaMatFilter.py
6OperateAllFiles.py
7dictionaryStatistics.py
8familienKonto.py
9makeDoc.py
10plotTimeSeries.py
11readData2DictOfArrays.py
12replaceInFile.py
13showPointOnCIExy.py
14svg2pdf.py
15testNumpyMatrix.py
16writeDictOfArrays.py
17writeTSV.py

Der gesamte Sourcecode darf gemäß GNU General Public License weiterverbreitet werden.