Issue
I have list of dict with UTF-8
and I want to save it in txt
file
ls_dict = [
{ 'a': 'میلاد'},
{ 'b': 'علی'},
{ 'c': 'رضا'}
]
I want it to save in csv
or txt
with UTF-8
Solution
You just need to make sure you specify the relevant encoding when you create/open the output file.
import json
ls_dict = [
{ 'a': 'میلاد'},
{ 'b': 'علی'},
{ 'c': 'رضا'}
]
with open('j.json', 'w', encoding='utf-8') as j:
j.write(json.dumps(ls_dict))
Subsequently...
with open('j.json', encoding='utf-8') as j:
j = json.load(j)
print(j)
Output:
[{'a': 'میلاد'}, {'b': 'علی'}, {'c': 'رضا'}]
Answered By - Stuart Answer Checked By - Mildred Charles (PHPFixing Admin)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.