As for CSV and TSV, they're a dead-simple format to read and write, and you can enforce a standard way to escape special characters if you aren't dealing with arbitrary user-submitted files. With numeric data escaping isn't even necessary, and if you have to deal with strings that might themselves contain special characters, the ASCII control characters 0x1E and 0x1F work well as alternatives to comma/tab and newline.
I know Python has a csv module, but I've just gotten used to writing these snippets:
def tsv_write(path, headers, data, sep='\t', end='\n'):
with open(path, 'w') as file:
file.write(sep.join(map(str, headers)) + end)
for datum in data:
file.write(sep.join(map(str, datum)) + end)
def tsv_read(path, headers, sep='\t', end='\n'):
with open(path, 'r') as file:
for line in file:
yield line.rstrip(end).split(sep)
(That tsv_read function only works if end == '\n'. Here's a somewhat-inefficient general-purpose alternative, although I've never needed it.) def read_records(file, end='\n'):
if end == '\n':
for line in file:
yield line
else:
record = []
while True:
c = file.read(1)
if c:
record.append(c)
if c == end or not c:
yield ''.join(record)
record = []
if not c:
break
def tsv_read(path, headers, sep='\t', end='\n'):
with open(path, 'r') as file:
for record in read_records(file, end):
yield record.rstrip(end).split(sep)