0

on macOS I've prepared python script with pandas, which reads csv file to dataframe and writes DF to file as JSON. The csv is xlsx saved as csv.

CSV contains strings like "Sławek" which appears in json as "S\u0142awek".

What can be done about it, to still have in JSON letters not their codes ?

the code is:

import pandas
import os 

files = [f for f in os.listdir('.') if os.path.isfile(f)]
for f in files:
    if f.find('csv')>=0 and f.find('~')<0:
            excel_data_df = pandas.read_csv(f, header=0, delimiter=';') 
            print(excel_data_df)
            json_str = excel_data_df.to_json(orient='records') 
            print(json_str)
            f = open(f.replace('csv','json'), "w")
            f.write(json_str)
            f.close() 

print(excel_data_df) prints "ł" print(json_str) prints "\u0142"

MPAW
  • 1,353
  • 1
  • 9
  • 17
  • you didn't provide the code you used, so I am guessing here. Check that you added `encoding="utf-8"` in the `open()` function containing the `json.dump(file, ensure_ascii=False)` – Adam Jan 22 '21 at 07:48
  • Added code. I don't think problem with dump is associated. – MPAW Jan 22 '21 at 08:54
  • Inside of `f = open(f.replace('csv','json'), "w")` add `encoding="utf-8"` as follows: `f = open(f.replace('csv','json'), "w", encoding="utf-8")` – Adam Jan 22 '21 at 09:04
  • Thank you Adam. I have found out also that json_str = excel_data_df.to_json(orient='records',force_ascii=False) makes it work ok – MPAW Jan 22 '21 at 09:08

0 Answers0