0

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?

3
Contributors
2
Replies
3
Views
6 Years
Discussion Span
Last Post by pyTony
0

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()

Edited by group256: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.