c24b
c24b

Reputation: 5552

No Newlines in the CSV when the field of the Dict is empty [Python, CSV]

I'm a python beginner, (I actually use the 2.7 version)

My code is actually working well (it's a simple one) but it insert new lines between the reslut in my output file. I've noticed that they mentionned it in the documentation (see further) but no the way to avoid inserting automatically a newline when a field is empty.

Anyone who could help me?

new= csv.DictReader(ifile,fieldnames=None)
output = csv.DictWriter(ofile,fieldnames=None,dialect='excel',delimiter="\t")


for item in new:
    output.writer.writerow(item.values())
    print item

Changed in version 2.5: The parser is now stricter with respect to multi-line quoted fields. Previously, if a line ended within a quoted field without a terminating newline character, a newline would be inserted into the returned field. This behavior caused problems when reading files which contained carriage return characters within fields. The behavior was changed to return the field without inserting newlines. As a consequence, if newlines embedded within fields are important, the input should be split into lines in a manner which preserves the newline characters.

Results:

{'Url;Site Source;Communaute': 'lafranceagricole.fr;Site Media;'} {'Url;Site Source;Communaute': 'economiesolidaire.com;Blog;'} {'Url;Site Source;Communaute': 'arpentnourricier.org;Blog;'} {'Url;Site Source;Communaute': 'arpentnourricier.org;Blog;'} {'Url;Site Source;Communaute': 'mamienne.canalblog.com;Blog;'}

So I have an error my header is becoming the key and not the simple header as I wanted (Url; Site source; Communauté) {lafranceagricole.fr; Site Media}

No blank line in my csv so the new line might not be produced by a blank field. Maybe the field Community by defaut empty create the new line

Upvotes: 0

Views: 458

Answers (1)

agf
agf

Reputation: 176920

If you want the ; to be the field delimiter, you have to specify that:

new = csv.reader(ifile, delimiter=';')
output = csv.writer(ofile,dialect='excel',delimiter="\t")
for item in new:
    output.writerow(item)

This works just as well with a DictReader to separate the fields.

Writes the fields tab delimited; I don't see any blank rows at all.


Old answer from before the question was clarified

It's not clear to me why you're using a DictReader to read your input, if you only want the cell values, not the field names. However, if for some reason you do need to use a DictReader:

from itertools import chain
output.writer.writerow(list(chain.from_iterable(item.values() for item in new)))

Will write all of the values, tab separated, into the output file, with no newlines in between.

If you don't really need a DictReader,

new = csv.reader(ifile)
output = csv.writer(ofile,dialect='excel',delimiter="\t")

next(new) # to skip the field names
output.writerow(list(chain.from_iterable(new)))

Will again write the cells as one row, tab separated, into the output file.

Upvotes: 1

Related Questions