0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

csvファイルをpandasで読み書き(自分用)

Posted at

#csvのファイルを都合の良いように作り直す
##目的
私がよく使う家計簿アプリのcsv出力後に、自分の欲しい情報だけを抽出して、新しいcsvに出力する

import locale
import os
import pandas as pd
import glob
import time
df = pd.read_csv('zaim10.csv',encoding='Shift-JIS',header=0)

df = df[['カテゴリの内訳','品目','お店','支出']]
data = []
#c1,c2,c3はカテゴリごとの合計金額を出力するための変数、salesは全合計
c1=0
c2=0
c3=0
sales = 0

# print(f'df["支出"]')
# print(df['支出'])

for row in df.values:
    row[2] = row[2].replace('-','関西スーパー')
    #そのまま出力すると\u3000が出てくるのでreplaceを使って置き換える
    row[2] = row[2].replace('\u3000',' ')
    row[2] = row[2].replace('ボトルワールドOK&業務スーパー 奈良店', '業務スーパー')

    print(f'-行:{row}')
    data.append(row)
    #dataはreplace関数を使った後のリスト表示を格納する
    sales += row[3]
    if('食料品'==row[0]):
        c1+=row[3]
    if('朝ご飯' == row[0]):
        c2+=row[3]
    if('晩ご飯' == row[0]):
        c3 += row[3]
print(f'支出金額合計 :{sales}')
print(f'食料品金額合計 :{c1}')
print(f'朝ご飯金額合計 :{c2}')
print(f'晩ご飯金額合計 :{c3}')
de = pd.DataFrame(data)#pandasのdata.objectになおすため。これをしないとcsvに書き出しの作業がpandasで出来ない
encoding = locale.getpreferredencoding()

de.to_csv('test.csv',encoding=encoding,index=False,header=0)#localeをimportせずにencoding="utf_8_sig"でも行ける
#indexは行番号、index_labelは行番号の表示、headerは先頭行の開始位置。

##引用一覧
https://teratail.com/questions/104353

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?