前提・実現したいこと
参考書に記載されている例題を実行したいのですが、dict(separate)で何か問題があるようで、対処方法がわかりません。
発生している問題・エラーメッセージ
C:\Users\beret\AppData\Local\Programs\Python\Python37-32\python.exe C:/Users/beret/PycharmProjects/SampleProject/Word_Dictionaly_New.py
Traceback (most recent call last):
File "C:/Users/beret/PycharmProjects/SampleProject/Word_Dictionaly_New.py", line 39, in <module>
read()
File "C:/Users/beret/PycharmProjects/SampleProject/Word_Dictionaly_New.py", line 23, in read
words = dict(separate)
ValueError: dictionary update sequence element #0 has length 1; 2 is required
Process finished with exit code 1
該当のソースコード
import time
words = {}
def read():
global words
with open('data/English_words.txt', 'r', encoding='utf_8') as file: lines = file.readlines() new_lines = [] for line in lines: line = line.rstrip('\n') if line != '': new_lines.append(line) separate = [] for line in new_lines: sp = line.split('\t') separate.append(sp) words = dict(separate)
def study():
write_lines = []
for key, val in words.items():
write_lines.append(key + '\t' + val + '\n')
write_lines.sort() with open('data/English_words.txt', 'w', encoding='utf_8') as f: f.writelines(write_lines)
if name == 'main':
read() print('単語の意味を回答します。') while True: word = input('英単語を入力してください-->') if word == 'OK': study() print('ありがとうございました。') break elif word in words: print('「' + words[word] + '」という意味です') else: print('理解できませんでした。新たな単語の場合、意味を入力してください。終了する場合は「OK」と入力してください。') meaning = input('意味を入力してください-->') while not meaning: meaning = input('意味を入力してください-->') words[word] = meaning print('記憶中です。お待ちください。') time.sleep(3)
試したこと
ファイル書きこみなしのプログラムの方は問題なく実行できました。
補足情報(FW/ツールのバージョンなど)
ここにより詳細な情報を記載してください。
回答1件
あなたの回答
tips
プレビュー