I want to basically do this:
f = open(genes_path, 'w')
for key, genes in key_genes.iteritems():
f.write(key)
for gene in genes:
f.write(",\t"+gene)
f.write("\n")
f.close()
And get this:
key1, AT3G32920, AT3G33187, AT3G32940, AT3G32930, AT3G32980, AT3G32960
key2, AT3G32920, AT3G33187, AT3G32940, AT3G32930,
Where the key can be any string (without a comma), order doesn't matter anywhere (I'm using the OrderedMultiDict from boltons and lists for printing convenience but it really doesn't matter, could be dict and set for all I care), and each row can have a different number of elements.
I can't seem to find any module that does this pretty simple task. DictWriter requires column/field names so this doesn't answer my problem. Numpy only works with rectangular arrays and padding introduces too much unnecessary stuff. I know it's easy to write the loop yourself, but I just feel like this is something common enough it would have it's own builtin.
For the times I need to just send people big lists of things (like genes to somebody who doesn't program) so they can pull it into excel add or remove elements then send it back and I don't have to do anything else.
Anyone know of a module that has functionality for automatically reading and writing these ragged dict-of-lists files? Or if there is a good reason for this not to exist?
I'm thinking something as dead-simple as pandas.read_csv(path, delimiter=",")
and pandas.DataFrame.to_csv(path, delimiter=",")
.
Rationale
The reason I am being picky about it being a single function of a module and not something I could very easily do in pure python isn't because I'm lazy, but because when you use something from a module with good documentation it is a lot easier for someone to look at the code and figure out exactly what was intended. Even if the task is kind of trivial you're still reducing complexity of your code. I see writing your own function as something domain-specific, whereas a common read-write routine should be something you import
and should preferably be used if available. Part of the zen of python right? So the second question really is asking "Is this a domain-specific task?", because it doesn't seem so to me.