1

I'm trying to read a text file contains dictionaries separated by comma, and convert to a list of dictionaries.

How can I do this with python?

I tried to read as json file or use split method

{
    "id": "b1",
    "name": "Some Name" 
},
{
    "id": "b2",
    "name": "Another Name"
},
....

result should be:

[ {"id" : "b1", "name" : "Some Name"} , {"id" : "b2", "name" : "Another Name"}, .... ]

Til
  • 5,150
  • 13
  • 26
  • 34
Cheng Zheng
  • 31
  • 1
  • 4
  • 1
    The main issue is that your data file isn't in a proper JSON format. You should make an edit to the source to wrap the list in square brackets `[`, `]` – flakes Jan 27 '19 at 03:39
  • Related / possible duplicate: [How to add a character at the end of a JSON-Object](https://stackoverflow.com/questions/47675367/how-to-add-a-character-like-at-the-end-of-a-json-object) – jpp Jan 27 '19 at 03:40

3 Answers3

2

If your file is not too big, you can do the following:

import json

with open('filename.txt', 'r') as file:
    result = json.loads('[' + file.read() + ']')
iz_
  • 15,923
  • 3
  • 25
  • 40
  • I don't think memory should be too much of an issue here. The built in JSON module is already not memory efficient. If memory consumption is a concern then OP should be using another library / approach. – flakes Jan 27 '19 at 03:38
  • As a minor side note, ensure that your final dict structure in the file doesn't have a trailing comma. Otherwise, you'll see something like `ValueError: No JSON object could be decoded`. – Chris Larson Jan 27 '19 at 03:51
0

You can use json module in python

JSON (JavaScript Object Notation), specified by RFC 7159 (which obsoletes RFC 4627) and by ECMA-404, is a lightweight data interchange format inspired by JavaScript object literal syntax (although it is not a strict subset of JavaScript).

json exposes an API familiar to users of the standard library marshal and pickle modules. https://docs.python.org/2/library/json.html

In your case if your file is a valid json file then you can use json.loads() method directly

import json

with open('test.txt', 'r') as file:
    result = json.loads(file.read())
rohit prakash
  • 565
  • 6
  • 12
  • The example in his question is _not_ valid json. And this doesn't address his desire to create a list of dicts. See @Tomothy32's answer. – Chris Larson Jan 27 '19 at 03:54
0

Welcome to Stack Overflow..!

If you are using a JSON file to store data, then I highly recommend the Python JSON Library. How to use it you ask..? Read this

If you plan on using a Text file to store data, I recommend that you store the data a bit differently than the JSON format

b1,Some Name
b2,Another Name
.
.

This would make reading the text file way easier. Just use the split() command to separate the lines from one another and then use split(",") on each of the lines to separate the ID from the Name.

Here is the code:

list = open("filename.txt").read().split("\n")
dictionaryList = []
for item in list:
    id, name = item.split(",")
    dictionaryList.append({id:name})

Hope this works..! SDA

Sagar
  • 404
  • 3
  • 14
  • There's no reason to "store the data a bit differently than the JSON format", JSON is completely fine and very widely used. (However, OP's data is not valid JSON.) – iz_ Jan 27 '19 at 04:12
  • That was if he was to use a Text file rather than JSON. He has mentioned that he wants a **list of dictionaries** and not a JSON as output – Sagar Jan 27 '19 at 04:45
  • JSON can be a list of dictionaries. – iz_ Jan 27 '19 at 04:48