问题
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