Ich habe einige Python-Code gedacht, um einige Daten zu übernehmen und eine Zeichenfolge aus diesen Daten neu erstellen, aber ich erhalte einen Fehler.ValueError: Wörterbuch-Aktualisierungssequenzelement # 6 hat die Länge 1; 2 ist erforderlich
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)
Fehler:
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
Sieht aus wie Element Nummer 6 hatte kein '=' drin. – TigerhawkT3
Die Liste mit Elementen darin ist: 'this = 0', 'is = 1', 'a = 2', 'einfach = 3', 'test = 4', 'file = 5', ', = 6 ',' im = 7 ',' gerade = 8 ',' using = 9 ',' it = 10 ',' bis = 11 ',' my = 13 ',' code = 14 ','. = 15 ' , 'i = 16', 'Hoffnung = 17', 'das = 18', "es wird = 19", "Arbeit = 20", "mit = 21", "mehr = 22", "als = 23" , 'one = 24', 'line = 25', 'weil = 26', 'ander = 27', 'weise = 28', '; = 29', 'be = 31', 'sehr = 32', ' diffcult = 33 ','! = 37 ' – Netoil
Warte mal; Warum parst du die String-Repräsentation einer 'list' (' indexdict') manuell statt mit'ast.literal_eval() '? Bitte drucken Sie 'lines [2]' und 'lines [1]' und editieren Sie diese in Ihre Frage. – TigerhawkT3