简体   繁体   English

值错误:字典更新序列元素 #6 的长度为 1; 2 是必需的

[英]ValueError: dictionary update sequence element #6 has length 1; 2 is required

I have some Python code meant to take some data and recreate a string from that data, but I'm receiving an error.我有一些 Python 代码旨在获取一些数据并从该数据重新创建一个字符串,但我收到一个错误。

def remove_cruft(s):
     return s[1:-2]

import re

filetouse = input("What file would you like to use?\n>>>")
filetouse = filetouse + ".txt"
f = open(filetouse,"r")
lines = f.readlines()
indexlist = lines[2]
indexdict = lines[1]

indexdict = indexdict.split()
indexdict.remove("['")
indexdict.remove("']") 
for item in indexdict:
    if "'," in indexdict:
        indexdict.remove("',")
    if "'" in indexdict:
        indexdict.remove("'")
    if '",' in indexdict:
        indexdict.remove('",')
    if '"' in indexdict:
        indexdict.remove('"')

indexdict = str(indexdict)
indexdict = indexdict[1:-1]
indexdict.replace(" ", "")

dict(x.split('=') for x in indexdict.split(','))

print(indexlist)
print(indexdict)
newindexlist = remove_cruft(indexlist)
newindexlist = re.findall(r"[\w']+|[.,!?;]", newindexlist)
del newindexlist[1::2]
print(newindexlist)

posindexdict = 0
finaloutput = []

print(finaloutput)

Error:错误:

Traceback (most recent call last):
  File "\\IOTA\ControlledAssessment\assess87\My Documents\Python\Task 3\Decompression v1.3.py", line 31, in <module>
    dict(x.split('=') for x in indexdict.split(','))
ValueError: dictionary update sequence element #6 has length 1; 2 is required

One of the parameter options for dict is an iterable of iterables of which each has exactly two objects: dict的参数选项之一是可迭代对象的可迭代对象,其中每个对象正好有两个对象:

>>> dict((('a', 1), ('b', 1)))
{'a': 1, 'b': 1}

Your code is trying to pass in an iterable where one of the sub iterables has only one object:您的代码试图传入一个可迭代对象,其中一个子可迭代对象只有一个对象:

>>> dict((('a', 1), ('b')))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: dictionary update sequence element #1 has length 1; 2 is required

Based on the output it seems that 7th element in your data is not in the format you expect.根据输出,数据中的第 7 个元素似乎不是您期望的格式。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ValueError:字典更新序列元素 #0 的长度为 1; 2 是必需的 Python 字典 - ValueError: dictionary update sequence element #0 has length 1; 2 is required Python Dictionary ValueError:字典更新序列元素#0的长度为4; 2是必需的(python) - ValueError: dictionary update sequence element #0 has length 4; 2 is required (python) Python-ValueError:字典更新序列元素#0的长度为15; 2个为必填项 - Python - ValueError: dictionary update sequence element #0 has length 15; 2 is required ValueError:字典更新序列元素#0的长度为1; 2个为必填项 - ValueError: dictionary update sequence element #0 has length 1; 2 is required / admin / login /处的ValueError-字典更新序列元素#0的长度为0; 2个为必填项 - ValueError at /admin/login/ - dictionary update sequence element #0 has length 0; 2 is required 鼻子测试错误:ValueError:字典更新序列元素#0的长度为4; 2个为必填项 - nosetest error: ValueError: dictionary update sequence element #0 has length 4; 2 is required ValueError:字典更新序列元素#0的长度为1; 2是必需的 - ValueError: dictionary update sequence element #0 has length 1; 2 is required Python ValueError:字典更新序列元素 #4 的长度为 3; 2 是必需的 - Python ValueError: dictionary update sequence element #4 has length 3; 2 is required 3 ValueError:字典更新序列元素#0的长度为3; 2个为必填项1 - 3 ValueError: dictionary update sequence element #0 has length 3; 2 is required 1 FLASK-ValueError:字典更新序列元素#0的长度为1; 2个为必填项 - FLASK - ValueError: dictionary update sequence element #0 has length 1; 2 is required
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM