简体   繁体   中英

Python CSV - Double-double quotes being written into CSV file?

I'm trying to write some data into a csv file. I want only the middle column to have quotes in the csv file. I start out with the data saved in a array over arrays. Here are a few entries printed out:

['1', '"For Those About To Rock We Salute You"', 'Album']
['2', '"Balls to the Wall"', 'Album']
['3', '"Restless and Wild"', 'Album']
['4', '"Let There Be Rock"', 'Album']
['5', '"Big Ones"', 'Album']
['6', '"Jagged Little Pill"', 'Album']
...

As you can see the only the middle column has quotes. However when I write this to a csv file this is what I get:

1,""For Those About To Rock We Salute You"",Album
2,""Balls to the Wall"",Album
3,""Restless and Wild"",Album
4,""Let There Be Rock"",Album
5,""Big Ones"",Album
6,""Jagged Little Pill"",Album
...

Everything is fine except that middle column! I have double-double quotes!

I have a function that takes the data (which is saved in an array of arrays), and writes it into a csv file. I looked into the QUOTE_NONE method but that doesn't seem to work...

file_data = ...
def write_node_csv():
    with open("./csv_files/albums.csv", mode='w') as csv_file:
        writer = csv.writer(csv_file, delimiter=',', quoting=csv.QUOTE_NONE, escapechar="\"")
        for data in file_data:
            writer.writerow(data)
    csv_file.close()

So basically I'm expecting this:

1,"For Those About To Rock We Salute You",Album
2,"Balls to the Wall",Album
3,"Restless and Wild",Album
4,"Let There Be Rock",Album
5,"Big Ones",Album
6,"Jagged Little Pill",Album
...

but I'm getting this:

1,""For Those About To Rock We Salute You"",Album
2,""Balls to the Wall"",Album
3,""Restless and Wild"",Album
4,""Let There Be Rock"",Album
5,""Big Ones"",Album
6,""Jagged Little Pill"",Album
...

Here's a demonstration of getting your results:

In []:
data = """'1', '"For Those About To Rock We Salute You"', 'Album'
'2', '"Balls to the Wall"', 'Album'
'3', '"Restless and Wild"', 'Album'
'4', '"Let There Be Rock"', 'Album'
'5', '"Big Ones"', 'Album'
'6', '"Jagged Little Pill"', 'Album'"""

import csv
with StringIO(data) as fin:
    reader = csv.reader(fin, quotechar="'", skipinitialspace=True)
    for row in reader:
        print(row)

Out[]:
['1', '"For Those About To Rock We Salute You"', 'Album']
['2', '"Balls to the Wall"', 'Album']
['3', '"Restless and Wild"', 'Album']
['4', '"Let There Be Rock"', 'Album']
['5', '"Big Ones"', 'Album']
['6', '"Jagged Little Pill"', 'Album']

In []:
with StringIO(data) as fin, StringIO() as fout:
    reader = csv.reader(fin, quotechar="'", skipinitialspace=True)
    writer = csv.writer(fout, quotechar='', quoting=csv.QUOTE_NONE)
    writer.writerows(reader)
    contents = fout.getvalue()
print(contents)

Out[]:
1,"For Those About To Rock We Salute You",Album
2,"Balls to the Wall",Album
3,"Restless and Wild",Album
4,"Let There Be Rock",Album
5,"Big Ones",Album
6,"Jagged Little Pill",Album

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM