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
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.
Comments