How do I convert this string into a dict in python?

dwstein

I have a string that I get from a mongodb export into a csv: Here's an example:

[{"group":"Most Common","id":"00000000000002","name":"HVAC Specialists","name_singular":"HVAC Specialist"},{"group":"Other","id":"00000000001","name":"Hauling \u0026 Junk Removers","name_singular":"Hauling \u0026 Junk Remover"}]

I'm trying to put the date into a dict so I can pull out what I need and export it.

I tried putting in into a list, but it created elements out of each letter.

The string is in a txt file and I'm calling it like this:

python stringwork.py thestring.txt

I saw this question and tried this:

from sys import argv
import json

# filename = argv

txt = open(argv[1])

json.loads(str(txt))

And I got the following error.

ValueError: No JSON object could be decoded
zhenguoli

The txt variable is a file object and can't be converted to a string. So you should read the contents of the file object such as txt = open(argv[1]).readlines(). Then you can obtain the string in the txt list with txt[0].

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from sys import argv
import json

filename = argv

txt = open(argv[1]).readlines()

a = json.loads(str(txt[0]))
print(a)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How should i convert this string into dict in python?

is the following a valid json ? how do I convert it into a dict in python

How do I convert a string into bytes in python?

How do I convert a string into code in Python?

How do I convert a string to math in python?

How can I convert string to dict or list?

How to convert a Python String to a dict without eval

how to convert string to dict with double quotes in python?

How do I parse Dict of {String, Dict of {String, Variant}} with QDBus?

How do I convert a string into a string of bytes in python 2.7

How do I convert a string to a string literal in python?

How to convert a python dict to a string with "beautified" dict formatting?

How do I convert a string to an escape sequence in Python?

How do I convert a list of ascii values to a string in python?

How do I convert a list into a string with spaces in Python?

How do I convert a number to a string in the same format as the input in python?

How do I convert a string from an input into an integer? (Python 3.9)

How do i convert a ascii python string to its trinary representation

How do I convert a list of integers to a string - Python

How do I convert a bytes object to a string in python?

How do I convert a string response size to MB in Python?

How do I replace a char in a dict in python

How do I split list of dict in Python?

How do I can get path for dict in dict in Python?

Convert a python dict to a string and back

how do i convert the value from str to int in my dict

How do I convert this complex nested Dict into Pandas

How to convert string with dict into actual dict object?

How to convert this json string to dict?