Empty CSV Python Script Line

I have a CSV file that has

spam

. 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 what I want?

+4
source share
2 answers

If you only need what you said, there is no need for a module csv:

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

In the CSV module, use the parameters delimiter, quotecharand quoting=csv.QUOTE_MINIMALfor the 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 contain:

spam
something
something else

Tested in Python 3.4.

+2
source

Source: https://habr.com/ru/post/1685482/


All Articles