Reading csv file in Python with different line terminator

I have a CSV file where the separator is the ASCII ^_ separator, and the line terminator is the ASCII ^^ separator (obviously, since these are non-printable characters, I just used one of the standard ways to write them). I wrote a lot of code that reads and writes CSV files, so my problem is not related to the Python csv module as such. The problem is that the csv module does not support reading lines (but supports lines), except for carriage return or lines, at least with Python 2.6, where I just tested it. The documentation says that this is because it is hardcoded, which I mean is done in the C code that underlies the module, since I did not see anything in the csv.py file that I could change.

Does anyone know a way to limit this restriction (patch, another CSV module, etc.)? I really need to read in a file where I cannot use carriage return or newlines as a line terminator because these characters will appear in some of the fields, and I would like, if possible, not to write my own reader code, although it was It would be quite simple to meet my needs.

+4
source share
1 answer

Why not put a custom iterable function csv.reader ? The following is a naive implementation that immediately reads the contents of a CSV file into memory (which may or may not be desirable, depending on the file size):

 def records(path): with open(path) as f: contents = f.read() return (record for record in contents.split('^^')) csv.reader(records('input.csv')) 

I think this should work.

+3
source

All Articles