Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass regex to delimiter field in python's csv module or numpy's genfromtxt / loadtxt?

Tags:

python

csv

numpy

I have tabulated data with some strange delimination (i.e. groups of values separated by commas, seperated from other values by tabs):

A,345,567   56  67  test

Is there a clean and clever way of handling multiple delimiters in any of the following: csv module, numpy.genfromtxt, or numpy.loadtxt?

I have found methods such as this, but I'm hoping there is a better solution out there. Ideally I'd like to use a genfromtxt and a regex for the delimiter.

like image 955
ryanjdillon Avatar asked Oct 06 '22 13:10

ryanjdillon


1 Answers

I’m afraid the answer is no in the three packages you asked for. However, you can just do replace('\t', ',') (or the reverse). For example:

from StringIO import StringIO # py3k: from io import StringIO
import csv
with open('./file') as fh:
    io = StringIO(fh.read().replace('\t', ','))

reader = csv.reader(io)

for row in reader:
    print(row)
like image 59
Chris Warrick Avatar answered Oct 10 '22 01:10

Chris Warrick