Hi, im quite new to python and I am trying to extract some data from a csv file and put it into a new file, this is the first line of the data:

1,3,0,1,2,2,1,13,19,30,13,10,20,+1

I have managed to get it to print a new line per column, but i need all this on one line, and it needs to be in the following format:

+1 1:1 2:3 3:0 4:1 5:2 6:2 7:1 8:13 9:19 10:30 11:13: 12:10 13:20

i currently have the following code:

import csv

ifile  = open('trainingSet1.csv', "rb")
f = open('train', "wb")
reader = csv.reader(ifile)

for row in reader:
 num = 0
 for col in row:
   c = num, ':', col
   print >>f, c
   num += 1  

ifile.close()

can anyone help?

You need to modify your design to avoid complication, somehow, I wrote a code that does what you want.

import csv
import sys

ifile  = open('trainingSet1.csv', "rb")
f = open('train', "wb")
reader = csv.reader(ifile)

last = ''
myString = ''

for row in reader:
    num = 1
    for col in row:
        if '+' in col:
            last = col
            num += 1
            continue
        myString += str(num) + ":" + str(col) + " "
        num += 1 
sys.stdout.write(last + " " + myString)

ifile.close()

bug at line 20. Debug bug ;) See line 5.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.