LoginSignup
1
5

More than 3 years have passed since last update.

csvファイルを読み込んで書き込む

Posted at
import csv

with open('test.csv', 'w+', newline='') as csv_file:
    fieldnames = ['Name', 'Count']
    writer = csv.DictWriter(csv_file, fieldnames=fieldnames)
    writer.writeheader()
    writer.writerow({'Name':'A', 'Count':1})
    writer.writerow({'Name':'B', 'Count':2})

    csv_file.seek(0)

    reader = csv.DictReader(csv_file)

    for row in reader:
        print(row['Name'], row['Count'])
実行結果
A 1
B 2
1
5
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
1
5