Python Forum
Delimiter issue with a CSV file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Delimiter issue with a CSV file
#1
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 ?
Reply
#2
A bit more searching and testing. Line 14 should be

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


Possibly Related Threads…
Thread Author Replies Views Last Post
  python Read each xlsx file and write it into csv with pipe delimiter mg24 4 1,475 Nov-09-2023, 10:56 AM
Last Post: mg24
  Context-sensitive delimiter ZZTurn 9 1,515 May-16-2023, 07:31 AM
Last Post: Gribouillis
  Read csv file with inconsistent delimiter gracenz 2 1,208 Mar-27-2023, 08:59 PM
Last Post: deanhystad
  File handling issue GiggsB 4 1,450 Mar-31-2022, 09:35 PM
Last Post: GiggsB
  How can I solve this file handling issue? GiggsB 17 3,594 Feb-14-2022, 04:37 AM
Last Post: GiggsB
  How to solve this file handling issue? GiggsB 3 1,705 Jan-10-2022, 09:36 AM
Last Post: Gribouillis
  How to create new line '/n' at each delimiter in a string? MikeAW2010 3 2,854 Dec-15-2020, 05:21 PM
Last Post: snippsat
  saving data from text file to CSV file in python having delimiter as space K11 1 2,415 Sep-11-2020, 06:28 AM
Last Post: bowlofred
  copy content of text file with three delimiter into excel sheet vinaykumar 0 2,364 Jul-12-2020, 01:27 PM
Last Post: vinaykumar
  Writing to File Issue Flash_Stang 3 2,537 Jun-05-2020, 05:14 AM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020