Pound sign appears incorrectly in Excel when written with Pandas.to_csv

Viewed 49

I have an array of arrays like so:

[["£", 1],["3", "9"]]

I'm using this Python code:

import pandas as pd

data = [["£", 1],["3", "9"]]
fields = ["a", "b"]
pd.DataFrame(data=data, columns=fields).to_csv("output.csv")

Upon opening in some text editor you will see the output is like so:

,a,b
0,£,1
1,3,9

But if you open in Excel the £ appears as enter image description here (I have no clue why this is a picture, but, we move!). If I use the command:

pd.DataFrame(data=data, columns=fields).to_excel("output.xlsx")

it appears correctly. But, I need it as a .csv and without the accented A when opened in Excel.

What have I tried:

  • I have tried to set the encoding to no luck
  • I tried looking at the source code of both functions, again to no luck
  • I tried using .to_excel("output.csv"), as I expected this raised an Exception

I wonder if it is maybe an issue with Excel itself (since it's fine when opened with Rons Editor, Notepad etc....)

0 Answers
Related