Varun
Varun

Reputation: 61

Python- How to use csv.writer with out quotation

I have a list which whose contents have to be written to a csv file, with out quotation.

The contents of the list is as shown below:

list=[(1, 2, 1), (1, 3, 1), (1, 4, 1), (1, 5, 1), (2, 1, 1), (2, 3, 1), (2, 4, 1), (2, 5, 1), (3, 1, 1), (3, 2, 1), (3, 4, 1), (3, 5, 1), (4, 1, 1), (4, 2, 1), (4, 3, 1), (4, 5, 1), (5, 1, 1), (5, 2, 1), (5, 3, 1), (5, 4, 1)]

Now i have the below two code snippet which tries to write this list to a csv file:

Method 1:

def method1():

list=[(1, 2, 1), (1, 3, 1), (1, 4, 1), (1, 5, 1), (2, 1, 1), (2, 3, 1), (2, 4, 1), (2, 5, 1), (3, 1, 1), (3, 2, 1), (3, 4, 1), (3, 5, 1), (4, 1, 1), (4, 2, 1), (4, 3, 1), (4, 5, 1), (5, 1, 1), (5, 2, 1), (5, 3, 1), (5, 4, 1)]
myfile = open('output.csv', 'w')
        wr = csv.writer(myfile, quoting = csv.QUOTE_NONE, delimiter=',', escapechar=',')
        wr.writerow(list)

The contents of the csv file are as shown below:

(1,, 2,, 1),(1,, 3,, 1),(1,, 4,, 1),(1,, 5,, 1),(2,, 1,, 1),(2,, 3,, 1),(2,, 4,, 1),(2,, 5,, 1),(3,, 1,, 1),(3,, 2,, 1),(3,, 4,, 1),(3,, 5,, 1),(4,, 1,, 1),(4,, 2,, 1),(4,, 3,, 1),(4,, 5,, 1),(5,, 1,, 1),(5,, 2,, 1),(5,, 3,, 1),(5,, 4,, 1)

I do not want the "," in between the elements of the list. If i remove the escapechar form the above code, it throws me an error.

The error is:

_csv.Error: need to escape, but no escapechar set

Method 2:

def method2():

list=[(1, 2, 1), (1, 3, 1), (1, 4, 1), (1, 5, 1), (2, 1, 1), (2, 3, 1), (2, 4, 1), (2, 5, 1), (3, 1, 1), (3, 2, 1), (3, 4, 1), (3, 5, 1), (4, 1, 1), (4, 2, 1), (4, 3, 1), (4, 5, 1), (5, 1, 1), (5, 2, 1), (5, 3, 1), (5, 4, 1)]
myfile = open('output.csv', 'wb')
        wr = csv.writer(myfile)
        wr.writerow(list)

The output of method 2 is as shown below:

"(1, 2, 1)","(1, 3, 1)","(1, 4, 1)","(1, 5, 1)","(2, 1, 1)","(2, 3, 1)","(2, 4, 1)","(2, 5, 1)","(3, 1, 1)","(3, 2, 1)","(3, 4, 1)","(3, 5, 1)","(4, 1, 1)","(4, 2, 1)","(4, 3, 1)","(4, 5, 1)","(5, 1, 1)","(5, 2, 1)","(5, 3, 1)","(5, 4, 1)"

Could anybody help me out with the issue?

The contents of my output file must look like this:

(1, 2, 1), 
(1, 3, 1), 
(1, 4, 1), 
(1, 5, 1),

...
...

Upvotes: 2

Views: 101

Answers (2)

monkut
monkut

Reputation: 43832

The format you describe doesn't seem to require the csv module. You can just write out the format using something like this:

list=[(1, 2, 1), (1, 3, 1), (1, 4, 1), (1, 5, 1), (2, 1, 1), (2, 3, 1), (2, 4, 1), (2, 5, 1), (3, 1, 1), (3, 2, 1), (3, 4, 1), (3, 5, 1), (4, 1, 1), (4, 2, 1), (4, 3, 1), (4, 5, 1), (5, 1, 1), (5, 2, 1), (5, 3, 1), (5, 4, 1)]

with open("output.csv", "w") as out_f:
    for item in list:
        output_str = "{},\n".format(item)
        out_f.write(output_str)

Upvotes: 2

xecgr
xecgr

Reputation: 5193

You don't need csv at all, look at this:

>>> list=[(1, 2, 1), (1, 3, 1), (1, 4, 1), (1, 5, 1), (2, 1, 1), (2, 3, 1), (2, 4, 1), (2, 5, 1), (3, 1, 1), (3, 2, 1), (3, 4, 1), (3, 5, 1), (4, 1, 1), (4, 2, 1), (4, 3, 1), (4, 5, 1), (5, 1, 1), (5, 2, 1), (5, 3, 1), (5, 4, 1)]
>>> with open('out.txt', 'w') as out:
...     for t in list:
...         out.write(str(t)+",\n")
... 
>>> 
host:~$ head out.txt 
(1, 2, 1),
(1, 3, 1),
(1, 4, 1),
(1, 5, 1),
(2, 1, 1),
(2, 3, 1),
(2, 4, 1),
(2, 5, 1),
(3, 1, 1),
(3, 2, 1),

Upvotes: 2

Related Questions