PaulyShore PaulyShore - 10 days ago 5
Python Question

subtracting square wave from csv values

I have a .csv file of time and resistance values with non-periodic pulses I need to remove.

This is what I'm trying but can't make the list elements into floats to do the comparison.

import csv

f=open('sample.csv')
csv_f=csv.reader(f)

res = []

next(csv_f)

for row in csv_f:
res.append([float(row[1])])

l = len(res)
previous = current = None

for i, r in enumerate(res):
if i > 0:
current = float(res[i])
previous = float(res[i-1])
dif = current-previous
if dif > 1:
res[i] = res[i] - dif


ideally I'd like to continue to subtract this difference until I detect another edge indicating the end of the pulse

with this code I get this error trying to cast the list element as a float

TypeError: float() argument must be a string or a number

if I remove the cast then it says

TypeError: unsupported operand type(s) for -: 'list' and 'list'

Answer

You have quite a few too many conversions going on. Honestly, the best thing that would have helped you is finding out

type(res[i])

(either through the interactive shell or a debugger like pdb).

You probably think res[i] is a float, but instead, it's a list. Here's why.

res.append([float(row[1])])

This line is appending into res, a (single-element) list consisting of the float value from row[1]. If you look at res (i.e. print(res)), you'll see something like this: [[1.2], [2.3], [3.14], [1.714]], when what I expect you want is simply [1.2, 2.3, 3.14, 1.714].

To fix this, just remove the square brackets:

res.append(float(row[1]))

Once you do this, you don't need any of your other float conversions:

import csv

f=open('sample.csv')
csv_f=csv.reader(f)

res = []

next(csv_f)

for row in csv_f:
    res.append(float(row[1]))

l = len(res)
previous = current = None

for i, r in enumerate(res):
    if i > 0:
        current = res[i]
        previous = res[i-1]
        dif = current-previous
        if dif > 1:
            res[i] = res[i] - dif
Comments