Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use python csv module for splitting double pipe delimited data

I have got data which looks like:

"1234"||"abcd"||"a1s1"

I am trying to read and write using Python's csv reader and writer. As the csv module's delimiter is limited to single char, is there any way to retrieve data cleanly? I cannot afford to remove the empty columns as it is a massively huge data set to be processed in time bound manner. Any thoughts will be helpful.

like image 895
Devesh Avatar asked Jun 15 '11 02:06

Devesh


People also ask

How do I use delimiter in Python CSV?

CSV Files with Custom DelimitersBy default, a comma is used as a delimiter in a CSV file. However, some CSV files can use delimiters other than a comma. Few popular ones are | and \t .


1 Answers

The docs and experimentation prove that only single-character delimiters are allowed.

Since cvs.reader accepts any object that supports iterator protocol, you can use generator syntax to replace ||-s with |-s, and then feed this generator to the reader:

def read_this_funky_csv(source):
  # be sure to pass a source object that supports
  # iteration (e.g. a file object, or a list of csv text lines)
  return csv.reader((line.replace('||', '|') for line in source), delimiter='|')

This code is pretty effective since it operates on one CSV line at a time, provided your CSV source yields lines that do not exceed your available RAM :)

like image 153
Pavel Repin Avatar answered Oct 05 '22 23:10

Pavel Repin