SantoshGupta7
SantoshGupta7

Reputation: 6197

Is there a work around for using multi-character delimiters for `csv.reader`?

Currently, only one character is allowed

Dialect.delimiter A one-character string used to separate fields. It defaults to ','.

https://docs.python.org/3.6/library/csv.html#csv.Dialect.delimiter

Is there a work around for multi-character delimiters ?

I am asking because I am working with very messy text data, which has pretty much instances of every sort of characters, so I need a rare combination of characters to effectively seperate values.

Upvotes: 2

Views: 8224

Answers (1)

martineau
martineau

Reputation: 123413

Here's the first part of my answer to the question CSV writing strings of text that need a unique delimiter adapted to work in Python 3.7:

import csv

DELIMITER = chr(255)
data = ["itemA", "itemB", "itemC",
        "Sentence that might contain commas, colons: or even \"quotes\"."]

with open('data.csv', 'w', newline='') as outfile:
    writer = csv.writer(outfile, delimiter=DELIMITER)
    writer.writerow(data)

with open('data.csv', 'r', newline='') as infile:
    reader = csv.reader(infile, delimiter=DELIMITER)
    for row in reader:
        print(row)

Upvotes: 1

Related Questions