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?

Community
  • 1
  • 1
ragzputin
  • 397
  • 3
  • 5
  • 20

2 Answers2

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.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • how can I delete a row? – ragzputin Jun 21 '16 at 06:35
  • 1
    @ragzputin: why do you need to delete it? You only need to *skip* the row and process the rest. – Martijn Pieters Jun 21 '16 at 06:36
  • @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. – Martijn Pieters Jun 21 '16 at 06:38
  • 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 Jun 21 '16 at 06:41
  • @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. – Martijn Pieters Jun 21 '16 at 06:52
  • I am only setting up the data so it can be plotted by some other program. Thanks for your input though. it was very useful :) – ragzputin Jun 21 '16 at 07:04
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) );
ch3ll0v3k
  • 336
  • 3
  • 9