0

enter image description here

I am getting value error in python while trying to convert a text file in to dictionary.

I am getting the file from an api.

filename=open('/sbranch/CTK/SAP/hkeep/vrp.json','r')
dictionary = {}
with open("/sbranch/CTK/SAP/hkeep/vrp.json", "r") as file:
    for line in file
        key, value = line.strip()
        dictionary[key] = value
    print(dictionary)

below is the error message:

key, value = line.strip()

ValueError: need more than 1 value to unpack

Peyman Mohamadpour
  • 17,954
  • 24
  • 89
  • 100
  • Could you please share the content of file vrp.json. As per your code `key, value = line.strip()` each line should have two values separated by a comma like 2, Name so 2 will become key and Name will value. – Alpha 1 Jun 05 '20 at 11:53
  • { "type" : "taskSap", "actions" : { "abortActions" : [ ], "emailNotifications" : [ ], "setVariableActions" : [ { "description" : null, "exitCodes" : null, "notificationOption" : "Operation Failure", "notifyOnEarlyFinish" : false, "notifyOnLateFinish" : false, "notifyOnLateStart" : false, "status" : "Failed", "variableName" : "jobname", "variableScope" : "Global", "variableValue" : {""} }, { "description" : null, "exitCodes" : null, "notificationOption" : "Operation Failure" – Janakiraman Jun 05 '20 at 11:56
  • its just begining of the file.. it has 100s of lines like this – Janakiraman Jun 05 '20 at 11:56
  • 1
    why cant you use `json.loads()` or `json.load()`? – Shijith Jun 05 '20 at 11:57
  • @Shijith, I am getting the below error when i tried with json.load() json.loads(json_data) File "/usr/lib64/python2.7/json/__init__.py", line 338, in loads return _default_decoder.decode(s) File "/usr/lib64/python2.7/json/decoder.py", line 366, in decode obj, end = self.raw_decode(s, idx=_w(s, 0).end()) File "/usr/lib64/python2.7/json/decoder.py", line 382, in raw_decode obj, end = self.scan_once(s, idx) ValueError: Expecting : delimiter: line 16 column 28 (char 464) – Janakiraman Jun 05 '20 at 12:02
  • https://stackoverflow.com/questions/28223242/python-json-loads-valueerror-expecting-delimiter – Shijith Jun 05 '20 at 12:10
  • I am just starting to write the python code.. i am really couldn't follow the logic in the forum. Could be please give me in a simple example.. how i should read that – Janakiraman Jun 05 '20 at 12:32

1 Answers1

0

This is a simple function to open a json file and load it into a python dictionary:

import json

def open_json(path):
    with open(path, 'r') as file:
        return json.load(file)

mydict = open_json('/sbranch/CTK/SAP/hkeep/vrp.json')

If you are getting the file from an api, you can actually get it directly using the requests library:

Install:

pip install requests

This will give you a dictionary from the json response (if there is json):

import requests

url = 'enter_your_url_here'
response = requests.get(url)
mydict = response.json()
bherbruck
  • 2,167
  • 1
  • 6
  • 17
  • First code worked for me and the dictionary is created successfully. I have printed the contents of dictionary and i noticed additional character is being added. {u'systemOperations': [{u'agentClusterVar': None, u'triggerVar': None, u'taskLimitType': u'Unlimited', u'variables': [], u'agent': None, u'notifyOnLateFinish': False, u'virtualResourceVar': None, u'variablesUnresolved': False, u'operation': u'Launch Task', u'execWorkflowName': None, u'execWorkflowNameCond': u'Equals', u'execId': None, u'notifyOnLateStart': False, not sure what is u' is meant and how can i remove that – Janakiraman Jun 05 '20 at 18:38
  • that's unicode encoding, what version of python, what version are you using? https://docs.python.org/2/tutorial/introduction.html#unicode-strings – bherbruck Jun 05 '20 at 18:58
  • I would HIGHLY recommend not using python 2.7 especially for starting a new project unless it is absolutely necessary, use python 3 instead: https://www.python.org/doc/sunset-python-2/ are you using PyCharm? – bherbruck Jun 06 '20 at 03:31
  • Thanks a lot for your support, i have changed to python3 shebang and it worked now. – Janakiraman Jun 06 '20 at 07:14
  • I have converted all my python 2 development to python3 version. Thanks for your suggestion. @Tenacious B – Janakiraman Jun 06 '20 at 15:36