I have this dataset in an excel spreadsheet and I have converted into a csv file for python to read:
1 5 0 1 3 2 1 18 30 50 13 12 24 1
0 1 0 0 1 1 1 10 10 12 10 6 16 -1
0 7 0 0 4 4 1 21 30 46 19 11 25 1
0 1 0 0 1 1 1 2 4 3 4 2 5 -1
0 1 0 0 1 1 1 4 4 7 3 6 6 -1
0 1 0 0 1 1 1 3 3 3 3 3 4 -1
2 1 0 0 1 1 1 8 7 12 5 5 12 -1
2 5 0 0 2 2 1 24 20 45 14 12 28 -1
2 5 0 0 3 3 1 14 17 21 9 12 18 -1
0 5 0 0 2 2 1 17 12 25 8 13 19 1
23 25 0 22 13 12 11 112 125 240 39 27 165 1
From this I need an output file in python format which prints in lists without the 0's. In other words, each row or column should not have any zero's. Please look below at the example format that should be printed.
+1 1:0.708333 2:1 3:1 4:-0.320755 5:-0.105023 6:-1 7:1 8:-0.419847 9:-1 10:-0.225806 12:1 13:-1
-1 1:0.583333 2:-1 3:0.333333 4:-0.603774 5:1 6:-1 7:1 8:0.358779 9:-1 10:-0.483871 12:-1 13:1
+1 1:0.166667 2:1 3:-0.333333 4:-0.433962 5:-0.383562 6:-1 7:-1 8:0.0687023 9:-1 10:-0.903226 11:-1 12:-1 13:1
-1 1:0.458333 2:1 3:1 4:-0.358491 5:-0.374429 6:-1 7:-1 8:-0.480916 9:1 10:-0.935484 12:-0.333333 13:1
-1 1:0.875 2:-1 3:-0.333333 4:-0.509434 5:-0.347032 6:-1 7:1 8:-0.236641 9:1 10:-0.935484 11:-1 12:-0.333333 13:-1
-1 1:0.5 2:1 3:1 4:-0.509434 5:-0.767123 6:-1 7:-1 8:0.0534351 9:-1 10:-0.870968 11:-1 12:-1 13:1
+1 1:0.125 2:1 3:0.333333 4:-0.320755 5:-0.406393 6:1 7:1 8:0.0839695 9:1 10:-0.806452 12:-0.333333 13:0.5
+1 1:0.25 2:1 3:1 4:-0.698113 5:-0.484018 6:-1 7:1 8:0.0839695 9:1 10:-0.612903 12:-0.333333 13:1
+1 1:0.291667 2:1 3:1 4:-0.132075 5:-0.237443 6:-1 7:1 8:0.51145 9:-1 10:-0.612903 12:0.333333 13:1
EDIT:
import csv list_new = []
with open('testingSeta.csv') as csvfile:
for row in csv.reader(csvfile):
row.insert(0, row.pop())
list_new.append(row)
list_new.pop(0)
print list_new
There are no zeros on their own. This is what I have so far. Please help. Thank You
what row.insert(0, row.pop()) is actually moving the last element of the list to be the first and shifting the rest of the list to the right. Also list_new.pop(0) is removing the element you have just inserted. I suggest you to put some print statements to see what your code is doing at each step:
import csv
list_new = []
with open('testingSeta.csv') as csvfile:
for row in csv.reader(csvfile):
print row
row.insert(0, row.pop())
print row
list_new.append(row)
print list_new
list_new.pop(0)
print list_new
print list_new
to remove the occurrences of '0' in the list row you can do this:
import csv
list_new = []
with open('testingSeta.csv') as csvfile:
for row in csv.reader(csvfile):
print row
while '0' in row: row.remove('0')
print row
list_new.append(row)
print list_new
for row in list_new: print(row)