Editing csv file Python 3 - matching file content to the print output
Question: I am having an issue with getting the contents of created csv file to show up
as the print output.

Code:

    
    
    import csv
    
    with open('test.csv', 'w', newline='') as csvfile:
        filewriter = csv.writer(csvfile, delimiter=' ',
                                quotechar=',', quoting=csv.QUOTE_MINIMAL)
        filewriter.writerow(['Spam'] * 2 + ['Baked Beans'])
        filewriter.writerow(['Spam', 'Lovely Spam', 'Wonderful Spam'])
    
    with open('test.csv', newline='') as csvfile:
         filereader = csv.reader(csvfile, delimiter=' ', quotechar=',')
         for row in filereader:
             print(', '.join(row))
    

Output:

    
    
    Spam, Spam, Baked Beans
    Spam, Lovely Spam, Wonderful Spam
    

`test.csv` file:

    
    
    Spam Spam ,Baked Beans,
    Spam ,Lovely Spam, ,Wonderful Spam,
    

_Notice the weird commas._

Desired contents of `test.csv`

    
    
    Spam, Spam, Baked Beans
    Spam, Lovely Spam, Wonderful Spam
    


Answer: The reason you are getting this output is because you are using comma
`quotechar=','` to quote fields containing space which is your delimiter. That
being said what you want is use `,` as delimiter which is the default
[delimiter
value](https://docs.python.org/3.5/library/csv.html#csv.Dialect.delimiter).

    
    
    import csv
    
    with open('test.csv', 'w', newline='') as csvfile:
        filewriter = csv.writer(csvfile)
        filewriter.writerow(['Spam'] * 2 + ['Baked Beans'])
        filewriter.writerow(['Spam', 'Lovely Spam', 'Wonderful Spam'])
    

The same thing apply if you want to read your `csv` file.

