user11702296
user11702296

Reputation: 1

Getting Output of my Python code to a csv file

I am writing a python code to pull a list and give the output in a .CSV file . The code is splitting after each character instead of each name . The [Name] in the below code contains a list of names like : aeims aelog amscompatibilitytool

with open("csvtest.csv", "wb") as f:
    writer = csv.writer(f,delimiter='\t', quotechar='"', quoting=csv.QUOTE_MINIMAL)
    for namespace in namespaces:
        writer.writerows(namespace['name'])

Expected :

aeims
aelog
amscompatibilitytool
cgr

Result I am getting in csv file :

a
e
i
m
s
a
e
l
o
g
a
m

.... so on

Upvotes: 0

Views: 92

Answers (1)

trotta
trotta

Reputation: 1226

writerows writes multiple rows to your csv file:

Write all elements in rows (an iterable of row objects as described above) to the writer’s file object, formatted according to the current dialect.

Try to use writerow instead:

Write the row parameter to the writer’s file object, formatted according to the current dialect.

Like This:

with open("csvtest.csv", "wb") as f:
    writer = csv.writer(f,delimiter='\t', quotechar='"', quoting=csv.QUOTE_MINIMAL)
    for namespace in namespaces:
        writer.writerow(namespace['name'])

Upvotes: 1

Related Questions