Home > Blockchain >  Dataframe.to_csv(sep=";") ends with newline instead of ";", how to replace newli
Dataframe.to_csv(sep=";") ends with newline instead of ";", how to replace newli

Time:04-26

I have this dataframe

d = {'col1': [1, 0, 1, 1, 0, 0, 1]}
df = pd.DataFrame(data=d)

and want to turn it into a csv file with one line:

1;0;1;1;0;0;1;

I tried this:

df.to_csv("filepath/filename.csv", index=False, header=False, sep=";") 

and the result is:

1;0;1;1;0;0;1\n

How do I replace the newline with a semicolin before creating the csv file? I dont want to create, open it and then replace it.

CodePudding user response:

You can use line_terminator.

df.to_csv("filepath/filename.csv", index=False, header=False, sep=";", line_terminator=";") 

CodePudding user response:

sep is the field delimiter value. You can set the line terminator with line_terminator.

Try:

df.to_csv("filepath/filename.csv", index=False, header=False, sep=";", line_terminator="") 
  • Related