PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, July 25, 2022

[FIXED] how to make text file of List of Dict python

 July 25, 2022     json, python     No comments   

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)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing