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

≡放荡痞女 提交于 2021-02-16 16:59:08

问题


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.


回答1:


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)


来源:https://stackoverflow.com/questions/14012143/pass-regex-to-delimiter-field-in-pythons-csv-module-or-numpys-genfromtxt-loa

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!