前提・実現したいこと
Pythonでcsvファイルを読み込み、店舗(AとB)の
売り上げの合計を出したいのですが、なかなかうまくいきません。
発生している問題・エラーメッセージ
'int' object is not iterable
該当のソースコード
Python
1import csv 2csv_file2 = open("/Users/aeri/Documents/py/ka2.csv","r",encoding = "utf-8") 3counter = 0 4tenpo_cnt = 0 5tenpo_uri = 0 6tenpo2_cnt = 0 7tenpo2_uri = 0 8total = 0 9for row in csv.reader(csv_file2): 10 counter +=1 11 if counter > 1: 12 if row[1] == "A": 13 tenpo_cnt += 1 14 tenpo_uri += int(row[2]) 15 else: 16 tenpo2_cnt += 1 17 tenpo2_uri += int(row[2]) 18print("A:" + str(tenpo_cnt) + "店舗 売り上げ合計:" + sum(tenpo_uri) + "円") 19print("B:" + str(tenpo2_cnt) + "店舗 売り上げ合計:" + sum(tenpo2_uri) + "円")
試したこと
sumの部分を除くとAとBの店舗数が正しく表示されるのですが、合計を求めようとすると、
エラーが出てしまいます。
お手数ですが、よろしくお願いいたします。
補足情報(FW/ツールのバージョンなど)
python 3.5.2
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/11/02 11:56