[英]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.