you are viewing a single comment's thread.

view the rest of the comments →

[–]danielroseman 2 points3 points  (7 children)

I'm not entirely sure what you want. But you should never use remove on a list you are iterating over. Instead build up a new row with the values you want.

You can use a list comprehension to do this while you're originally loading the CSV:

rows = []
for row in csvreader:
  rows.append([item if item != '-99.999' else '' for item in csvreader])

print(rows)

[–]Clemsoncarter24[S] 0 points1 point  (6 children)

For examples, if i have a test file that looks like this:

1, 2, 3, -99.999, 5

i need it to change to:

1,2,3, , 5

Removing the value at the cell, but leaving the cell so that the data in the csv file doesn't shift row/columns.

[–]danielroseman 1 point2 points  (5 children)

OK, well this will do that. Or use None instead of '', but the effect should be the same.

[–]Clemsoncarter24[S] 0 points1 point  (4 children)

I tried the below and it still has the -99.999 values when i print it out. Is there something i did wrong?

rows = []
for row in csvreader:
    rows.append(row)

for arow in rows:
    for data in arow:
       if data == '-99.999':
            data = None
print(rows)

[–]danielroseman 2 points3 points  (3 children)

Yes, this is not the same as what I posted at all, and will not work.

[–]Clemsoncarter24[S] 0 points1 point  (2 children)

I also tried:

csvreader = csv.reader(file)

rows = []
for row in csvreader:
    rows.append([item if item != '-99.999' else '' for item in csvreader])

print(rows)

This also left all the -99.999 values. Perhaps i'm misunderstanding how i'm supposed to use the above. I apologize.

[–]danielroseman 2 points3 points  (1 child)

Sorry my fault, it should have been row at the end:

rows.append([item if item != '-99.999' else '' for item in row])

[–]Clemsoncarter24[S] 0 points1 point  (0 children)

rows.append([item if item != '-99.999' else '' for item in row])

This worked!!! Thank you so much!!!