Skip to content Skip to sidebar Skip to footer

Pandas To_csv With Multiple Separators

I want to convert a pandas dataframe to csv with multiple separators. Is there a way? dataframe.to_csv(file.csv, sep='%%') Error: delimiter must be 1-character string

Solution 1:

The easiest way might be to use a unique single-character separator first, then replace it:

tsv = dataframe.to_csv(sep='\t') # use '\1' if your data contains tabs
psv = tsv.replace('\t', '%%')
with open('file.csv', 'w') as outfile:
    outfile.write(psv)

P.S.: Consider using an extension other than .csv since it's not comma separated.


Solution 2:

I think there might be some bugs with replace as John says, cause it can't promise the replaced character is the seperator.

Besides, as to_csv returned as a string, if the data is big, it migth lead to memory error.

Here is another feasible solution.

with open('test_pandas.txt', 'w') as f:
    for index, row in dataframe.iterrows():
        l = map(str, row.values.tolist())
        line = '%%'.join(l)
        f.write(line+'\n')

Post a Comment for "Pandas To_csv With Multiple Separators"