清理Python中的返回数据 - xlswriter

时间:2017-08-04 14:01:33

标签: python xlsxwriter

我想帮助下面的python方法。如下所示,该函数返回列表中的一些数据。

def EIS(self):
    '''This function runs EIS experiment'''

    #some code here

    frequencies=[100000.0,79432.8,63095.7,50118.7,39810.7,31622.8,25118.9]
    Data = []
    for freq in (frequencies) :
        #print(freq)
        self.sendcommand("GS_SetBestCurrentRange", timeout=0.1)
        self.sendcommand("GS_FRASetModulationFrequency %.5f" %freq, timeout=0.1)
        self.sendcommand("GS_FRAGetMeasurementMinDurationMS", timeout=0.1)
        self.sendcommand("GS_FRAMakeMeasurement 1", timeout=0.1)
        self.sendcommand("GS_GetCurrentRange")
        VoltageSamples = self.sendcommand("PS_FRAGetVoltageSample", timeout=0.1)
        #CurrentSample = self.sendcommand("PS_FRAGetCurrentSample", timeout=0.1).split(" ")
        Data.append(VoltageSamples,)
        print (Data)
    return Data

我的问题是我没有正确格式的返回数据。你可以看到它返回的内容:

['GS_FRAGetVoltageSample 0.00699254 -213.701 0\r']
['GS_FRAGetVoltageSample 0.00699254 -213.701 0\r', 'GS_FRAGetVoltageSample 0.0128992 -187.322 0\r']

我想清除数据,所以它只能返回浮动数字(0.00699254 -213.701等...)我想让数字0.00699254和0.0128992在一列中,-213.701和-187.322在另一栏。我使用xlswriter库,我正在调用该函数:

Data= TEST.EIS()
worksheet4.write_column('A1',Data)

但它输出A1列中的所有数据,如此

"GS_FRAGetVoltageSample 0.00699254 -213.701 0"
"GS_FRAGetVoltageSample 0.0128992 -187.322 0"
"GS_FRAGetVoltageSample 0.0113329 -67.7349 0"
"GS_FRAGetVoltageSample 0.0110053 -63.2269 0"
"GS_FRAGetVoltageSample 0.0149435 -51.8437 0"
"GS_FRAGetVoltageSample 0.0131203 -38.0366 0"
"GS_FRAGetVoltageSample 0.0120906 -35.6771 0"

有关如何清理数据并让它们进入不同列的任何帮助吗?

1 个答案:

答案 0 :(得分:2)

您需要清理VoltageSamples

def EIS(self):
    ...
    Data = []
    for freq in frequencies:
        ...
        ...
        VoltageSamples = self.sendcommand . . . 
        samples = VoltageSamples.split()
        Data.append( (samples[1], samples[2]) )
    return Data

然后你需要插入两个不同的列表,一个列入A1,另一个列入A2:

Data = TEST.EIS()
print(Data)
list1, list2 = zip(*Data)

worksheet4.write_column('A1', list1)
worksheet4.write_column('B1', list2)

<强>更新

我已使用此测试代码验证上述建议:

frequencies=[31622.8, 25118.9]
Data = []
for freq in frequencies:
    VoltageSamples = ' '.join(('foo', str(freq), 'bar', 'zoo'))
    samples = VoltageSamples.split()
    Data.append( (samples[1], samples[2]) )

print(Data)
list1, list2 = zip(*Data)
print(list1)
print(list2)

我得到的输出是:

[('31622.8', 'bar'), ('25118.9', 'bar')]
('31622.8', '25118.9')
('bar', 'bar')