0

I have a list of dictionaries written to a data.txt file. I was expecting to be able to read the list of dictionaries in a normal way when I load, but instead, I seem to load up a string.

For example - when I print(data[0]), I was expecting the first dictionary in the list, but instead, I got "[" instead.

Below attached is my codes and txt file:

read_json.py

import json

with open('./data.txt', 'r') as json_file:
    data = json.load(json_file)
    print(data[0])

data.txt

"[
    {
        "name": "Disney's Mulan (Mandarin) PG13 *",
        "cast": [
            "Jet Li",
            "Donnie Yen",
            "Yifei Liu"
        ],
        "genre": [
            "Action",
            "Adventure",
            "Drama"
        ],
        "language": "Mandarin with no subtitles",
        "rating": "PG13 - Some Violence",
        "runtime": "115",
        "open_date": "18 Sep 2020",
        "description": "\u201cMulan\u201d is the epic adventure of a fearless young woman who masquerades as a man in order to fight Northern Invaders attacking China. The eldest daughter of an honored warrior, Hua Mulan is spirited, determined and quick on her feet. When the Emperor issues a decree that one man per family must serve in the Imperial Army, she steps in to take the place of her ailing father as Hua Jun, becoming one of China\u2019s greatest warriors ever."
    },
    {
        "name": "The New Mutants M18",
        "cast": [
            "Maisie Williams",
            "Henry Zaga",
            "Anya Taylor-Joy",
            "Charlie Heaton",
            "Alice Braga",
            "Blu Hunt"
        ],
        "genre": [
            "Action",
            "Sci-Fi"
        ],
        "language": "English",
        "rating": "M18 - Some Mature Content",
        "runtime": "94",
        "open_date": "27 Aug 2020",
        "description": "Five young mutants, just discovering their abilities while held in a secret facility against their will, fight to escape their past sins and save themselves."
    }
]"

The above list is formatted properly for easy reading but the actual file is a single line and the different lines are denoted with "\n". Thanks for any help.

3
  • Does the text file literally start with " or is it valid JSON? It's probably better if you post the exact text file contents without any embellishment so we can reproduce the problem and better assist. Commented Sep 16, 2020 at 6:47
  • @ggorlen the .txt file is written with json.dump. It starts with " and ends with a ". Commented Sep 16, 2020 at 6:52
  • delete the " literally from start and end and it should work just fine. Even if it is dumped by json, there should not be " literally at the start and end in the file. Commented Sep 16, 2020 at 9:17

1 Answer 1

2

remove double quote in data.txt is useful for me。

eg. modify

"[{...},{...}]"

to

[{...},{...}]

Hope it helps!

Sign up to request clarification or add additional context in comments.

2 Comments

It is better to use english on SO, which can be understand by more people.
Hi @Jin Liu, Thank you for your answer, the problem is solved. 谢谢您的答复看来没问题了。

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.