问题描述
我正在尝试使用Matlab先前生成的工作区来创建输入文本文件,以供另一个程序读取。我可以将工作区作为字典导入到Python中。从这里开始,由于字典中的数据类型和数组大小不同,我很难写入文本文件。我希望字典中的每个字段在文本文件中都有自己的列,但运气不好。 这是从matlab导入的字典的屏幕截图。
我可以使用以下代码将一个变量添加到文本文件中,但不能添加更多变量或具有不同数据类型的变量。
import csv
import scipy.io
import numpy as np
#import json
#import _pickle as pickle
#import pandas as pd
mat = scipy.io.loadmat('Day055.mat')
#print(type(mat))
#print(mat['CC1'])
CC1 = mat['CC1']
CC2 = mat['CC2']
DP1 = mat['DP1']
#print(CC1)
#print(CC2)
dat = np.array([CC1,DP1])
dat =dat.T
#np.savetxt('tester.txt',dat,delimiter = '\t')
np.savetxt('tester.txt',CC1,delimiter = '\t')
'''
with open('test.csv','w') as f:
writer = csv.writer(f)
for row in CC1:
writer.writerow(row)
#print(type(CC1))
#print("CC1=",CC1)
#print("first entry to CC1:",CC1[0])
mat = {'mat':mat}
df = pd.DataFrame.from_dict(mat)
print(df)
print(type(mat))
x=0
with open('inputDay055.txt','w') as file:
for line in CC1:
file.write(CC1[x])
#file.write("\t".join(map(str,CC2))+"\n")
#file.write(pickle.dumps(mat))
x=x+1
'''
print("all done")
如您所见,我也尝试了几种不同的方法,但是当我失败时将其注释掉。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)