我有一个类对象的字典。我想使用Python将类的成员值(时间点,拟合,度量)写入csv文件。
我的班级:
class PlotReadingCurves:
def __init__(self, timepoints, fitted, measured):
self.timepoints = timepoints
self.fitted = fitted
self.measured = measured
obj = PlotReadingCurves(mTimePoints,mFitted,mMeasured)
PlotReadingCurvesList[csoId] = obj
Eg: timpoints : 1 2 3 4 5
fitted: 6 7 8 9 10
measured: 11 12 13 14
预期结果:
timepoints fitted measured fitted measured
1 6 11 .. ..
2 7 12
3 8 13
4 9 14
5 10 15
答案 0 :(得分:1)
试试我的迷你包装库pyexcel
。虽然它没有熊猫那么强大,但只需几行代码就可以将一个dict写入excel文件:
>>> import pyexcel as pe
>>> your_dict = { "timepoints": [1,2,3], "fitted":[6,7,8]} # more columns omitted
>>> sheet = pe.Sheet(pe.utils.dict_to_array(your_dict))
>>> sheet.save_as("your_file_name.csv") # done
使用pyexcel
,您可以轻松地将数据写入其他excel格式:xls,xlsx甚至ods。可以找到文档here
答案 1 :(得分:0)
尝试使用pandas
,这是大熊猫有关您问题的功能。
用于在内存数据结构和不同格式之间读取和写入数据的工具:CSV和文本文件,Microsoft Excel,SQL数据库和快速HDF5格式;
它非常方便和强大。