Python learning day 4

Aggregate the contents of csv with Python

Purpose: Because the data acquired by scraping in the future was converted to csv, and the data was formatted and analyzed.

What to do this time: Display the conference room and the number of entries in csv

I'm a beginner, and I'm at the level of studying how to write Markdown. I thought the output was essential, so I'm thinking of leaving it for myself.

environment

pc:Macbook pro python3 ver3.8.6 pyq

File creation

Create the file using the terminal.

~$cd Documents

~Documents$mkdir study

~Documents$cd study

~Documents/study$touch room.csv

~Documents/study$touch study.py

~Documents/study$ls
room.csv        test.py

--Run--
~Documents/study$python3 study.py

room.csv


Meeting room A,Streak
Conference room B,Seven herbs
Conference room C,cross
Meeting room A,Yatsushiro
Meeting room A,Yotsuba
Meeting room A,Miya
Conference room B,Streak
Conference room B,Futaki
Conference room C,Rokutsuka
Meeting room A,cross
Conference room B,Nikaido
Conference room C,Nanase
Meeting room A,One color

stury.py


#List initialization
book = {}
#read csv
with open ('room.csv', encoding='utf-8') as f:

    #Meeting room A,Yamada
    for row in f:

        #Remove whitespace and list
        columns = row.rstrip().split(',')


        room = columns [0]

        #From the second time onwards ...
        if room in book:
           book[room] += 1
        
        #First time{'Meeting room A': 1, 'Conference room B': 1, 'Conference room C': 1}
        else:
            book[room] = 1

#keys values items items have both, so they have two arguments
#print(book) → {'Meeting room A': 6, 'Conference room B': 4, 'Conference room C': 3}

for room_name, count in book.items():
    print(room_name + ':' + str(count))




-----------result-----------

Meeting room A:6
Conference room B:4
Conference room C:3

Recommended Posts

Python learning day 4
python learning
Python day 1
Effective Python Learning Memorandum Day 15 [15/100]
Effective Python Learning Memorandum Day 6 [6/100]
Effective Python Learning Memorandum Day 9 [9/100]
Effective Python Learning Memorandum Day 8 [8/100]
Effective Python Learning Memorandum Day 14 [14/100]
Effective Python Learning Memorandum Day 1 [1/100]
Effective Python Learning Memorandum Day 13 [13/100]
Effective Python Learning Memorandum Day 3 [3/100]
Effective Python Learning Memorandum Day 5 [5/100]
Effective Python Learning Memorandum Day 4 [4/100]
Effective Python Learning Memorandum Day 7 [7/100]
Effective Python Learning Memorandum Day 2 [2/100]
[Python] Learning Note 1
Python learning notes
python learning output
Python learning site
Python Deep Learning
Python learning (supplement)
Deep learning × Python
python learning notes
Python study day 1
Python class (Python learning memo ⑦)
Learning Python with ChemTHEATER 03
"Object-oriented" learning with python
Learning Python with ChemTHEATER 05-1
Python: Deep Learning Practices
Python ~ Grammar speed learning ~
Python: Unsupervised Learning: Basics
Learning record 4 (8th day)
Learning record 9 (13th day)
[1day1lang AdventCalender] day4 Python
Learning record 3 (7th day)
Learning record 5 (9th day)
Learning record 6 (10th day)
Programming learning record day 2
Learning record 1 (4th day)
Learning record 7 (11th day)
Private Python learning procedure
Learning Python with ChemTHEATER 02
Learning Python with ChemTHEATER 01
Python: Deep Learning Tuning
Learning record 2 (6th day)
Python + Unity Reinforcement Learning (Learning)
Python: Supervised Learning (Regression)
Learning record 16 (20th day)
Learning record 22 (26th day)
Python: Supervised Learning (Classification)
Python exception handling (Python learning memo ⑥)
Learning record 13 (17th day) Kaggle3
Umemura style Python expedition Day 0
O'Reilly python3 Primer Learning Notes
<Course> Deep Learning: Day2 CNN
Learning flow for Python beginners
Learning record No. 10 (14th day)
Learning record No. 17 (21st day)
Python learning plan for AI learning
Learning record 12 (16th day) Kaggle2
Learning record No. 18 (22nd day)