how to write a csv file with arabic characters using python and pandas?

Viewed 762

I have a function that read csv or excel files and try to copy and paste or to read and write the file using pandas package.

When I try to write to a csv file a file with arabic characters it display symbols and non-understandable characters.

how can i specify the econding of the files that is utf-8 and be able to decode it ?

function :

import pandas as pd
import numpy as np
import base64

def download_file(df, types, new_types, extension):
    for i, col in enumerate(df.columns):
        new_type = types[new_types[i]]
        if new_type:
            try:
                df[col] = df[col].astype(new_type)
            except:
                st.write('Could not convert', col, 'to', new_types[i])
        # csv
    if extension == 'csv': 
        csv = df.to_csv(r"F:\AIenv\update2.csv",index=False,header=True,encoding="utf8")
        b64 = base64.b64encode(csv.encode()).decode("utf-8")
        
    else:
        excel = df.to_excel(r"F:\AIenv\update2.xlsx", index = False, header=True,encoding="utf-8")
        b64 = base64.b64encode(excel.encode()).decode("utf-8")
   

and here i am calling the function :

  if btn1:
    download_file(df, types, new_types, "csv")
  if btn2:
    download_file(df, types, new_types, "xls")

where is the error in my code ?

0 Answers
Related