1

Okay here I go! I am such a noob by the way and I am trying my best to learn. I had to write a little code to open a csv file and format the list such a way that it would work with Libsvm but anyway this is the code I wrote so far :

import csv
with open ('testingSeta.csv')as csvfile:
reader = csv.reader(csvfile, delimiter = ',')
for i in reader:
    i.insert (0, i.pop(13))
    print (" ".join(i))

and this gives me a list like this:

-1 0 1 1 0 1 1 1 4 5 6 5 5 8
-1 0 1 0 0 1 1 1 4 3 7 1 3 6
 1 3 7 2 0 4 4 1 41 46 86 20 18 48
 1 10 11 0 0 6 6 3 26 65 102 25 16 38

testingSeta.csv original format:

0,1,1,0,1,1,1,4,5,6,5,5,8,-1
0,1,0,0,1,1,1,4,3,7,1,3,6,-1
3,7,2,0,4,4,1,41,46,86,20,18,48,1
10,11,0,0,6,6,3,26,65,102,25,16,38,1

I would like the list to be ordered like this:

-1 1:0 2:1 3:1 4:0 5:1 6:1 7:1 8:4 9:5 10:6 11:5 12:5 13:8
-1 1:0 2:1 3:0 4:0 5:1 6:1 7:1 8:4 9:3 10:7 11:1 12:3 13:6
1 1:3 2:7 3:2 4:0 5:4 6:4 7:1 8:41 9:46 10:86 11:20 12:18 13:48
1 1:10 2:11 3:0 4:0 5:6 6:6 7:3 8:26 9:65 10:102 11:25 12:16 13:38

the numbering '1:' should always start from the second number/value. Can any body help? thanks for your time

3
  • Example content of testingSeta.csv would be ueseful. Commented Jan 11, 2015 at 1:12
  • done. added to the post Commented Jan 11, 2015 at 1:18
  • 1
    don't use insert simply slice, you should use also enumerate to index Commented Jan 11, 2015 at 1:53

3 Answers 3

1
with open('testingSeta.csv') as f:
   for line in csv.reader(f):
      print(' '.join(
                [line[-1]] + 
                ['{0}:{1}'.format(*x) for x in enumerate(line[:-1], start=1)]))


-1 1:0 2:1 3:1 4:0 5:1 6:1 7:1 8:4 9:5 10:6 11:5 12:5 13:8
-1 1:0 2:1 3:0 4:0 5:1 6:1 7:1 8:4 9:3 10:7 11:1 12:3 13:6
1 1:3 2:7 3:2 4:0 5:4 6:4 7:1 8:41 9:46 10:86 11:20 12:18 13:48
1 1:10 2:11 3:0 4:0 5:6 6:6 7:3 8:26 9:65 10:102 11:25 12:16 13:38
Sign up to request clarification or add additional context in comments.

Comments

1

try like this:

>>> with open('your_file') as f:
...     for x in f:
...         line = x.strip().split(',')
...         print "{} ".format(line[-1]) + " ".join("{}:{}".format(i,y) for i,y in enumerate(line[:-1],start=1))
... 
-1 1:0 2:1 3:1 4:0 5:1 6:1 7:1 8:4 9:5 10:6 11:5 12:5 13:8
-1 1:0 2:1 3:0 4:0 5:1 6:1 7:1 8:4 9:3 10:7 11:1 12:3 13:6
1 1:3 2:7 3:2 4:0 5:4 6:4 7:1 8:41 9:46 10:86 11:20 12:18 13:48
1 1:10 2:11 3:0 4:0 5:6 6:6 7:3 8:26 9:65 10:102 11:25 12:16 13:38

Comments

1
>>> with open ('testingSeta.csv') as csvfile:
...     reader = csv.reader(csvfile, delimiter = ',')
...     for i in reader:
...         last = i.pop(13)
...         for index,string in enumerate( i ) :
...             i[ index ] = str(index+1) + ":" + string
...         print last + " " + " ".join(i)
... 
-1 1:0 2:1 3:1 4:0 5:1 6:1 7:1 8:4 9:5 10:6 11:5 12:5 13:8
-1 1:0 2:1 3:0 4:0 5:1 6:1 7:1 8:4 9:3 10:7 11:1 12:3 13:6
1 1:3 2:7 3:2 4:0 5:4 6:4 7:1 8:41 9:46 10:86 11:20 12:18 13:48
1 1:10 2:11 3:0 4:0 5:6 6:6 7:3 8:26 9:65 10:102 11:25 12:16 13:38

4 Comments

OMG OMG OMG UPASANA! YOU ARE AN ANGEL! lankans go hard! thanks so much. much love.
@Ash You should not use insert a simple end, rest = row[-1:], row[:-1] will extract the last element and all up to the last, and enumerate is pretty much perfect for getting the indexes
@PadraicCunningham so I should use end, rest = row[-1:], row[:-1] instead of i.insert (0, i.pop(13))??
@Ash, sorry should be row[-1],row[:-1] row[-1] is the last row element or 13 in your code then row[:-1] is all up to not including the last.

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.