【发布时间】:2020-09-24 14:55:42
【问题描述】:
我在尝试将文本文件转换为字典时在 python 中遇到值错误。
我从 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)
以下是错误信息:
key, value = line.strip()
ValueError: 需要超过 1 个值才能解压
【问题讨论】:
-
能否分享一下文件vrp.json的内容。根据您的代码
key, value = line.strip(),每行应有两个值,由逗号分隔,如 2,名称,因此 2 将成为键,名称将成为值。 -
{ "type" : "taskSap", "actions" : { "abortActions" : [ ], "emailNotifications" : [ ], "setVariableActions" : [ { "description" : null, "exitCodes ":null,"notificationOption":"操作失败","notifyOnEarlyFinish":false,"notifyOnLateFinish":false,"notifyOnLateStart":false,"status":"Failed","variableName":"jobname","variableScope" : "Global", "variableValue" : {""} }, { "description" : null, "exitCodes" : null, "notificationOption" : "操作失败"
-
它只是文件的开头.. 它有 100 行这样的行
-
为什么不能使用
json.loads()或json.load()? -
@Shijith,当我尝试使用 json.load() json.loads(json_data) 文件“/usr/lib64/python2.7/json/__init__.py”时,出现以下错误,第 338 行,在加载中返回 _default_decoder.decode(s) 文件“/usr/lib64/python2.7/json/decoder.py”,第 366 行,在解码 obj 中,end = self.raw_decode(s, idx=_w(s , 0).end()) 文件“/usr/lib64/python2.7/json/decoder.py”,第 382 行,在 raw_decode obj 中,end = self.scan_once(s, idx) ValueError: Expecting : delimiter: line 16 列 28(字符 464)
标签: python