G Tsirigos
G Tsirigos

Reputation: 83

Clean return data in Python - xlswriter

I would like some help with the python method below. As you can see below the function returns some data in a list.

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

My problem is that the return data i m having do not have the right format.Below you can see what it returns:

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

I want to clear the Data so it can only return the floating numbers (0.00699254 -213.701 etc...) and I would like lets say numbers 0.00699254 and 0.0128992 to be in one column and -213.701 and -187.322 to be in another column. I use the xlswriter library and I'm calling that function:

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

but it outputs all the data in A1 column like this

"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"

Any help of how I can clean the data and make them to go in different columns?

Upvotes: 2

Views: 97

Answers (1)

quamrana
quamrana

Reputation: 39404

You need to clean up VoltageSamples:

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

Then you need to insert two different lists, one into A1, and another into A2:

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

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

Update:

I have used this test code to verify my suggestions above:

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)

The output I get is:

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

Upvotes: 2

Related Questions