Lost1
Lost1

Reputation: 1062

Python CSV writer blank line

I have a CSV file, which has

spam

in it. Then, i did

 with open(directory, "a") as config_csv:
    writer = csv.writer(config_csv)
    writer.writerow(["something"])
    writer.writerow(["something else"])

I expected

spam
something
something else

Instead, I got

spam

"something"

"something else"

How do I get the thing I want?

Upvotes: 9

Views: 8951

Answers (3)

Keine
Keine

Reputation: 45

Other answers in SE did not work for me. What I did was:

with open('ADC.csv', 'a') as csvfile:
    csv_Writer = csv.writer(csvfile, delimiter=';', lineterminator='\n')

Upvotes: 2

cuuupid
cuuupid

Reputation: 1002

With the CSV module, use delimiter, quotechar, and quoting=csv.QUOTE_MINIMAL options to desired effect:

import csv
with open(file, "a", newline='') as config_csv:
    writer = csv.writer(csvfile, delimiter=' ',
                            quotechar='|', quoting=csv.QUOTE_MINIMAL)
    writer.writerow(["something"])
    writer.writerow(["something else"])

file will then contain:

spam
something
something else

Tested on Python 3.4.

Upvotes: 5

francisco sollima
francisco sollima

Reputation: 8338

If what you need is just what you said, there's not need for the module csv:

with open(directory, "a") as config_csv:
    config_csv.write("something\n")
    config_csv.write("something else\n")

Upvotes: 2

Related Questions