Reputation: 542
with open('processedData/file.tsv', 'wb') as f:
dict_writer = csv.DictWriter(f, keys, delimiter = '\t')
dict_writer.writeheader()
dict_writer.writerows(hgnc_list)
By executing the above code, I'm successfully writing my list of dictionaries to a file.
The catch is
: that some of the values in the dictionaries are lists. Which I would like to be printed without the square brackets or the ''
and use the |
as a delimiter instead of the comma
so the output csv file would be like this:
column1 column2 column3 ... columnN
value11 value121|value122 value13 ... value1N1|value1N2|value1N3
Instead of:
column1 column2 column3 ... columnN
value11 ['value121','value122'] value13 ... ['value1N1','value1N2','value1N3']
Can this be done without reprocessing the string?
Upvotes: 0
Views: 698
Reputation: 251408
The csv
module (and CSV files in general) don't handle things like list entries in any special way. If you want to put a list into a CSV entry, you need to explicitly convert your values to strings, store the string values, and then parse them back into lists yourself when you read the data.
For instance, if you have some_list
, a list of strings, you could replace that in your dictionary with '|'.join(some_list)
to get a pipe-separated string, and then store that value. If you read that data back in, such an entry will be read as a single string value, which you'll have to split into a list yourself.
Upvotes: 1