input.csv
1,2,text, date, qwertyuiopasdfghjklñzxcvbnm, yhnujmik,2121212121

import csv

reader = csv.reader(open('input.csv', 'rb'), delimiter=',',quoting=csv.QUOTE_NONNUMERIC))
csv_out = csv.writer(open('output.csv', 'w'))


for row in reader:
	content = row[0] + row[4] +  row[5] +  row[11] +  row[12] + row[13] +  row[16] + row[17] + row[22]
	csv_out.writerow( content )

# I want it to save the file in csv format as: 1,2,yhnujmik,2121212121

however the code is generating this: 1,2,y,h,n,u,j,m,i,k,2,1,2,1,2,1,2,1,2,1

I've tried with different text delimiters, but doesnt seem to be the problem

any advice?

thanks guys!

content = row[0] + row[4] +  row[5] +  row[11] +  row[12] + row[13] +  row[16] + row[17] + row[22]
	csv_out.writerow( content )

The above uses 9 fields.

# I want it to save the file in csv format as: 1,2,yhnujmik,2121212121

This has 4 fields.

however the code is generating this: 1,2,y,h,n,u,j,m,i,k,2,1,2,1,2,1,2,1,2,1

This has 20 fields. 9 != 4 != 20. There is no way to answer a question that does not make sense. Post some sample input and desired output and perhaps we can then offer some help.

my bad, the code i got is:

input.csv
1,2,text, date, qwertyuiopasdfghjklñzxcvbnm, yhnujmik,2121212121

import csv

reader = csv.reader(open('input.csv', 'rb'), delimiter=',',quoting=csv.QUOTE_NONNUMERIC))
csv_out = csv.writer(open('output.csv', 'w'))


for row in reader:
	content = row[0] + row[1] +  row[5] +  row[6] 
	csv_out.writerow( content )

which is giving me: 1,2,y,h,n,u,j,m,i,k,2,1,2,1,2,1,2,1,2,1 instead of 1,2,yhnujmik,2121212121

thanks!

Looks like writer takes sequence of letters and thinks each letter is record, give it a sequence of strings instead.

1) QUOTE_NONNUMERIC doesn't work in the reader because your input file contains unquoted non numeric fields.
2) writerow() expects a sequence and separates its items with the delimiter. If you give it a string, it will separate single characters. The correct way to use it is to pass a list or a tuple containing your row items

def main(opts, args):
    reader = csv.reader(open('input.csv', 'rb'), delimiter=',')
    csv_out = csv.writer(open('output.csv', 'w'), delimiter=',')

    for row in reader:
        content = list(row[i] for i in (0,1,5,6))
    	csv_out.writerow( content )

my bad, the code i got is:

input.csv
1,2,text, date, qwertyuiopasdfghjklñzxcvbnm, yhnujmik,2121212121

import csv

reader = csv.reader(open('input.csv', 'rb'), delimiter=',',quoting=csv.QUOTE_NONNUMERIC))
csv_out = csv.writer(open('output.csv', 'w'))


for row in reader:
	content = row[0] + row[1] +  row[5] +  row[6] 
	csv_out.writerow( content )

which is giving me: 1,2,y,h,n,u,j,m,i,k,2,1,2,1,2,1,2,1,2,1 instead of 1,2,yhnujmik,2121212121

thanks!

Ok with Gribouillis.
Just to modify your code to make it work :

import csv

reader = csv.reader(open('input.csv', 'rb'), delimiter=','))
csv_out = csv.writer(open('output.csv', 'w'))


for row in reader:
	content = [row[0], row[1],  row[5],  row[6]]
	csv_out.writerow( content )

@Gribouillis: thank you that did the trick, works almost fine, due to it is writing the rows like this:

row1
<blank row>
row2
<blank row>
row3

is there anything we can do to make it write the rows continuosly?

import csv

reader = csv.reader(open('input.csv', 'rb'), delimiter=','))
csv_out = csv.writer(open('output.csv', 'wb'))


for row in reader:
	content = [row[0], row[1],  row[5],  row[6]]
	csv_out.writerow( content )

I changed the way i was opening the file to “wb” and worked just fine, I learned that if you dont open the file in binary mode it will add \r\r\n after each record!

commented: Nice info +13
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.