Python Forum

Full Version: Delimiter issue with a CSV file
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I'm trying to delete specific columns from a CSV file. The delete column works fine but the output of the columns remaining has changed. I need the column formats to be the same.

Input csv data: "Group","Title","URL","Notes"
ouput csv data: Group,Title,URL,Notes

Here is the code; I have only changed line 14

# Source from https://nitratine.net/blog/post/remove-columns-in-a-csv-file-with-python/
import csv

input_file = 'input.csv'
output_file = 'output.csv'
cols_to_remove = [6, 7, 8, 9] # Column indexes to be removed (starts at 0)

cols_to_remove = sorted(cols_to_remove, reverse=True) # Reverse so we remove from the end first
row_count = 0 # Current amount of rows processed

with open(input_file, "r") as source:
    reader = csv.reader(source)
    with open(output_file, "w", newline='') as result:
        writer = csv.writer(delimiter = '"', result)
        for row in reader:
            row_count += 1
            print('\r{0}'.format(row_count), end='') # Print rows processed
            for col_index in cols_to_remove:
                del row[col_index]
            writer.writerow(row)
Quote: File "remove_cols_csv3.py", line 14
writer = csv.writer(delimiter = '"', result)
^
SyntaxError: positional argument follows keyword argument

thinking that it was the delimiter that needed changing. yet the delimiter is fine, it is a comma input and a comma output.

What do I need to change in the code to ensure that each column (header and data) is encased in a double quote please ?
A bit more searching and testing. Line 14 should be

writer = csv.writer(result, quotechar='"', quoting=csv.QUOTE_ALL)
seems to work okay.