テキストファイルから読み込んだリストを辞書オブジェクトに変換したいです。呼び出したテキストの、¥nは消去できますが、そこから、がどうしてもできないです。
※#======の上は問題なく動作してます。
python3
1with open('data/English_words.txt', 'r', encoding='utf_8' 2 ) as file: 3 lines = file.readlines() 4 5new_lines = [] 6for line in lines: 7 line = line.rstrip('\n') 8 9 if line != '': 10 new_lines.append(line) 11#print(new_lines) 12 13#ここまでは問題なく動作してます。 14#======================================================================= 15separate = [] 16 17for line in new_lines: 18 sp = line.split('\t') #1行データをタブの所で分割 19 separate.append(sp) #分割したあとに2要素のリストをリストseparateに追加 20words = dict(separate) #dict()関数で辞書オブジェクトを作る。 21 22print(words)
English_words.txt
crucial
1subsequent その後の 2devise 考察する 3strain 負担 4distinct 明確な 5incorporate 取り入れる 6eliminate 排除する 7privilege 特権 8retain 記憶する 9seize 掴む 10perceive 知覚する 11prominent 著名な 12integrate 一体化する 13anticipate 予想する 14disturb 邪魔する 15respective それぞれ 16perspective 観点 17magnificent 壮大な
エラーコード
Traceback
1 File "C:/Users/beret/PycharmProjects/SampleProject/sm3.py", line 17, in <module> 2 words = dict(separate) 3ValueError: dictionary update sequence element #0 has length 1; 2 is required 4 5
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。