python csv register_dialect

Rinks picture Rinks · Dec 21, 2011 · Viewed 8.2k times · Source

I am using the csv module in python to parse a delimieted file. I use the register_dialect method of csv to specify the deleimiter and other parameters. It was working fine all this while, but now I have a file that uses 'tab' as delimiter. This obviously gives me an error - "TypeError: "delimiter" must be an 1-character string" on using '\t' as the delimiter. Is there a way to be able to use the tab character here? What are my options now?

Thanks!!

fileObject = open(fullFilePath,'rb')
csv.register_dialect('MyDialect', delimiter='\t',doublequote=False,quotechar='',lineterminator='\n',escapechar='',quoting=csv.QUOTE_NONE)
myReader = csv.reader(fileObject,'MyDialect')

I am using python 2.7.

Answer

dm03514 picture dm03514 · Dec 21, 2011

I have never used a register_dialect method but one can pass in a csv.reader has a delimiter argument that accepts a value of \t. http://docs.python.org/library/csv.html