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.
pc:Macbook pro python3 ver3.8.6 pyq
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