0

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

4
  • can you please format code more better. just select all code and click on format button. Commented Jun 26, 2019 at 9:48
  • Can you show us what your namespaces var looks like? Commented Jun 26, 2019 at 9:49
  • namespace is a class name which is pulling the list from a node Commented Jun 26, 2019 at 9:55
  • One more doubt , if i add another write statement , and want the output of that in the 2nd column of the csv file , is there a way to do that ? Commented Jun 26, 2019 at 10:48

1 Answer 1

1

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'])
Sign up to request clarification or add additional context in comments.

1 Comment

One more doubt , it i add another write statement , and want the output of that in the 2nd column of the csv file , is there a way to do that ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.