Dakshina Murthy
Dakshina Murthy

Reputation: 51

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

Upvotes: 4

Views: 2816

Answers (2)

DennisLi
DennisLi

Reputation: 4156

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')

Upvotes: 2

John Zwinck
John Zwinck

Reputation: 249123

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.

Upvotes: 4

Related Questions