Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can the CSV module parse files with multi-character delimiters?

Tags:

python

csv

I have a "CSV" file that uses a multi-character delimiter, so the data looks something like

field1_|#|_field2_|#|_field3_|#|_field4

Is there a way to use the Python CSV module to parse this file?

like image 574
zenzic Avatar asked Aug 10 '11 14:08

zenzic


People also ask

Is multiple character delimiter supported as record delimiter?

You can use multiple characters as a delimiter. The Oracle loader documentation says that the delimiter must be a single character, but it doesn't say anything about the field delimiter. So, you can use any number of characters for both delimiters.

Why do we use the csv module in Python over split?

Python CSV Module Python provides a CSV module to handle CSV files. To read/write data, you need to loop through rows of the CSV. You need to use the split method to get data from specified columns.

Which module is used for working with csv files?

Reading a CSV using Python's inbuilt module called csv using csv. reader object.

Which function is used to specify the delimiter in csv?

csv file format has data items separated by a delimiter other than a comma. This includes semicolon, colon, tab space, vertical bars, etc. In such cases, we need to use the sep parameter inside the read. csv() function.


1 Answers

Try to replace the multichar delimiter with the singlechar one.

Somethin like this:

class DelimitedFile:
  def __init__(self, fname, mode='rb', ind=',', outd=','):
    self.f = open(fname, mode)
    self.ind = ind
    self.outd = outd

  def __iter__(self):
    return self

  def next(self):
    line = self.f.next()
    return line.replace(self.ind, self.outd)

Use it like so:

import csv

delimiter = ','

reader = csv.reader(DelimitedFile(fileName, ind='_|#|_', outd=delimiter))

for row in reader:
  print row
like image 130
alexblum Avatar answered Nov 06 '22 22:11

alexblum