1

I have tried to find an answer to this problem but to no avail. Here's the problem I'm facing -

I have a csv file where some rows have three elements (indicating that they are full) and some other rows that only have one element in them (indicating they are not full and so should be discarded). Here's an example:

Time, Voltage, Charge
A, B, C
D, E, F
G, H, I
J,
K,
L,

As above, I need to remove J, K and L from my csv file. So far I've tried this approach (found in another stack overflow thread here):

if any(val not in (None,"") for val in row):
   battlog_voltage.append(float(row[1])/1000.0)

row1 is where the empty fields begin as seen beside J, K, L. However, I get the following error:

  File "/home/raghavk/Documents/batterylog.py", line 81, in <module>
    mydata = mylog.loadCycleData('battery-2.csv')

  File "/home/raghavk/Documents/batterylog.py", line 68, in loadCycleData
    battlog_voltage.append(float(row[1])/1000.0)

IndexError: list index out of range

What can I do to solve this problem?

2 Answers 2

1

The csv.reader object creates a new list for every row and does not pad these. Just test for the row length:

if len(row) > 1:

The question you link to uses csv.DictReader(), which is given fieldnames up-front, resulting in either None or '' empty string values.

Sign up to request clarification or add additional context in comments.

6 Comments

how can I delete a row?
@ragzputin: why do you need to delete it? You only need to skip the row and process the rest.
@ragzputin: you can always write a new CSV file with csv.writer(), but the solution there is the same, filter out any row that is too short by testing their length.
I need all the unnecessary data removed because this data will later be plotted. I don't wanna the rows with only one element in them.
@ragzputin: you didn't show how you are building the data structure for plotting. Again, you'd filter; only use rows that have more than 1 element in the list.
|
0

There is alot you can do.

One way is maybe this:

new_list = [];
for one_line in your_data_list:

    spl = one_line.split(",");

    if  len( spl ) < 3:# or < 2 if you need
       pass;
    else:
        new_list.append( spl) );

2 Comments

Don't use if test: pass then else: block. Just negate the test: if not test: block.
For len(something) < 3, negation becomes len(something) >= 3 or len(something) > 2.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.