Can Python CSV module parse multi-column delimited files

I have a โ€œCSVโ€ file that uses a multi-column separator, so the data looks something like

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

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

thanks

+6
source share
2 answers

Try replacing the multichromic splitter with a one-way splitter.

Something like that:

 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 this:

 import csv delimiter = ',' reader = csv.reader(DelimitedFile(fileName, ind='_|#|_', outd=delimiter)) for row in reader: print row 
+5
source

The Python csv module cannot handle separators with more than one character, so the short answer to the question: "Can the Python CSV module parse multi-column delimited files?" no. "A simple test confirmed this:

 reader = csv.reader(open('test.csv'), delimiter = '|#|') 

This led to an error:

TypeError: "delimiter" must be a 1 character string

(test.csv was a 2-line delimited file, as shown in the code.)

Thus, you will need to either replace the delimiters with separate character delimiters, as suggested by @alexblum, write your own parser, or find another parser. Googling 'python csv multi-character delimiter' showed hits to several.

+1
source

Source: https://habr.com/ru/post/894722/


All Articles