python3を学習している初心者です。
参考書の通りに打ち込んだのですが、実行すると
Traceback (most recent call last):
File "05_file.py", line 2, in <module>
raw_data = open_file.read()
AttributeError: 'str' object has no attribute 'read'
このようにエラーメッセージが出ます。
どうぞご教授をお願いしたいです。
下記のように定義いたしました。
open_file = ('point.txt')
raw_data = open_file.read()
open_file.close()
point_data = raw_data.splitlines()
point_dict = {}
for line in point_data:
student_name, points_str = line.split(':')
point_dict[student_name] = points_str
score_dict = {} for student_name in point_dict: point_list = point_dict[student_name].split(',') subject_number = len(point_list) total = 0 for point in point_list: total = total + int(point) average = total / subject_number score_dict[student_name] = (total, average, subject_number)
evaluation_dict = {}
for student_name in score_dict:
score_data = score_dict[student_name]
total = score_data[0]
average = score_data[1]
subject_number = score_data[2]
excellent = subject_number * 100 * 0.8 good = subject_number * 100 * 0.65 if total >= excellent: evaluation = 'Excellent' elif total >= good: evaluation = 'Good' else: evaluation = 'Bad' evaluation_dict[student_name] = evaluation
file_name = 'evaluation.txt'
output_file = open(file_name, '')
for student_name in score_dict:
score_data = score_dict[student_name]
total = score_data[0]
evaluation = evaluation_dict[student_name] text = '[{}] total: {}, evaluation: {}\n'.format(student_name, total, evaluation) output_file.write(text) output_file.close() print('評価結果を{}に出力しました'.format(file_name))
調べたところopen_fileを定義している場所でなにか間違いがあるとのことですが、まだ理解できていません。
Takanori Suzuki:100,88,81
Takayuki Shimizukawa:77,94,85
Mitsuki Sugiya:80,52,99
このように教本通り打ち込み
point.txtというものを同じ階層に保存しております。
どうぞよろしくお願い致します。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/10/30 14:53
2018/10/30 14:56 編集
2018/10/30 15:00