1

hi I have some problem.

import csv
test = ['a','b','c','d','e']

for t in test:
    with open('names.csv', 'a') as csvfile:

        fieldnames = ['acol', 'bcol', 'ccol']
        writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
        writer.writeheader()
        writer.writerow({'acol': t, 'bcol': t, 'ccol': t})

'names.csv' result

acol bcol ccol

a a a

acol bcol ccol

b b b

acol bcol ccol

c c c

acol bcol ccol

d d d

acol bcol ccol

e e e''

there is whiteline and colume repeat i want result without white rows and without colume repeat

DOMI DOMI
  • 33
  • 9

4 Answers4

3

You could try this

import csv
test = ['a','b','c','d','e']

with open('names.csv', 'wb') as csvfile:
    fieldnames = ['acol', 'bcol', 'ccol']
    writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
    writer.writeheader()
    for t in test:
        writer.writerow({'acol': t, 'bcol': t, 'ccol': t})
  • but... there is whiteline still – DOMI DOMI Feb 25 '16 at 12:36
  • the whiteline is probably an issue of importing it back. I do not see a whiteline if I reopen the file in e.g LibreOffice. – Moritz Feb 25 '16 at 12:38
  • The white line doesn't seem to appear for me. –  Feb 25 '16 at 12:41
  • mistake,,, not whiteline ---> white rows – DOMI DOMI Feb 25 '16 at 12:41
  • Replace this line `with open('names.csv', 'a') as csvfile:` to `with open('names.csv', 'wb') as csvfile:`. Check my updated answer. –  Feb 25 '16 at 13:20
  • i use ('names.csv', 'wb') this is error ``Traceback (most recent call last): self.writerow(header) File "C:\Python34\lib\csv.py", line 153, in writerow return self.writer.writerow(self._dict_to_list(rowdict)) TypeError: 'str' does not support the buffer interface >>> `` – DOMI DOMI Feb 25 '16 at 15:02
  • i use exel2007 version..and python3.4 – DOMI DOMI Feb 25 '16 at 15:06
  • For python 3.4 you need to encode the string before writing in to a file. checkout this link for python 3x string encoding http://stackoverflow.com/questions/5471158/typeerror-str-does-not-support-the-buffer-interface –  Feb 25 '16 at 15:19
1
import csv
test = ['a','b','c','d','e']

for i,t in enumerate(test):
    with open('names.csv', 'a') as csvfile:

        fieldnames = ['acol', 'bcol', 'ccol']
        writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
        if i == 0:
            writer.writeheader()
        writer.writerow({'acol': t, 'bcol': t, 'ccol': t})

The other solution provided by Divakar is more elgant and faster since you do not open the file in every loop.

Moritz
  • 5,130
  • 10
  • 40
  • 81
1

Use this

import csv
test = ['a','b','c','d','e']

for t in test:
    with open('names.csv', 'w') as csvfile:

        fieldnames = ['acol', 'bcol', 'ccol']
        writer = csv.writer(csvfile)
        writer.writerow(fieldnames)

        for elem in test:
            writer.writerow(elem * 3)

here is my output from names.csv it dosen't have any newline

acol,bcol,ccol
a,a,a
b,b,b
c,c,c
d,d,d
e,e,e
danidee
  • 9,298
  • 2
  • 35
  • 55
1
import csv
test = ['a','b','c','d','e']

with open('names.csv', 'a') as csvfile:
    fieldnames = ['acol', 'bcol', 'ccol']
    writer = csv.DictWriter(csvfile,delimiter=' ',fieldnames=fieldnames,lineterminator=' ')
    writer.writeheader()
    for t in test:
        writer.writerow({'acol': t, 'bcol': t, 'ccol': t})

you are writing headers inside the loop so they are rewritten many times,by default line terminator is '\n' which gives new white space at end of line.This gives the result you cited above

Eliethesaiyan
  • 2,327
  • 1
  • 22
  • 35
  • what do you mean?@DOMIDOMI i checked the question before it was edited,i thought the results posted before were what you wanted to accomplish,have you checked the lineterminator option? – Eliethesaiyan Feb 25 '16 at 12:48
  • acol bcol ccol a a a acol bcol ccol b b b acol bcol ccol c c c acol bcol ccol d d d acol bcol ccol e e e – DOMI DOMI Feb 25 '16 at 12:54
  • i understand...but did you add those modification.i mean copy and paste those code exactly...because i get different result here – Eliethesaiyan Feb 25 '16 at 12:55