Python learning (supplement)

A supplement to the python CSV module mentioned earlier.

Honestly, you can understand it by document or google, Keep it as a memorandum

If you want to open a CSV file and write it to another file as it is Use the reader method to pass in list format

At this time, the return value of the reader method is of course in list format.

import csv

data=[]

#Read CSV in list format
with open('data.csv' , 'r', newline='' , encoding='utf-8') as f:

    r = csv.reader(f)
    data = [ i for i in r ]

#Write CSV in list format
with open('data.csv' , 'w', newline='' , encoding='utf-8') as f:

    w = csv.writer(f)
    w.writerows(data)

At this time, the data is entered in the following multiple list.

[['python', '1'], ['php', '1']]

When writing with writerow, a list is passed as an argument, Note that you have to writerows for multiple lists like the one above.

If you try writerow, the return value will be as follows (returned as a one-line list)

"['python', '1']","['php', '1']"

If you do write rows, the return value will be as follows.

python,2
php,2

Use DictReader method to pass in dictionary format

At this time, the return value of the DictReader method is of course in dictionary format.

from collections import defaultdict
import csv

data = defaultdict(int)

with open('data.csv' , 'r', newline='' , encoding='utf-8') as f:

    r = csv.DictReader(f)
    data = [ i for i in r ]    #Store dictionary in list

with open('data.csv' , 'w', newline='' , encoding='utf-8') as f:
    fieldnames =['Name', 'Count']
    w = csv.DictWriter(f, fieldnames= fieldnames)
    w.writerows(data)

If you look inside the variables as in the case of the list, the data variables will be as follows

[{'Name': 'python', 'Count': '1'}, {'Name': 'php', 'Count': '1'}]

This time the keys and values come in as a dictionary

When writing, the list is passed as an argument to the writerow method, so Actually, it is an image of passing the contents of the dictionary in a list

If you try to write with writerow as in the case of list, Attribute Error will occur as follows I get angry that the list object doesn't have an attribute called key

AttributeError: 'list' object has no attribute 'keys'

If you use writerows, you can write as follows as in the case of list

python,1
php,1

I understand that the writerows method is used when passing both the basic list and the dictionary multiple times.

Recommended Posts

Python learning (supplement)
python learning
[Python] Learning Note 1
Python learning notes
python learning output
Python learning site
Python learning day 4
Python Deep Learning
Deep learning × Python
python learning notes
Python class (Python learning memo ⑦)
Learning Python with ChemTHEATER 03
"Object-oriented" learning with python
Python module (Python learning memo ④)
Reinforcement learning 1 Python installation
Learning Python with ChemTHEATER 05-1
Python: Deep Learning Practices
Python ~ Grammar speed learning ~
Python: Unsupervised Learning: Basics
Private Python learning procedure
Learning Python with ChemTHEATER 02
Learning Python with ChemTHEATER 01
Python: Deep Learning Tuning
Python + Unity Reinforcement Learning (Learning)
Python: Supervised Learning (Regression)
Python: Supervised Learning (Classification)
Effective Python Learning Memorandum Day 15 [15/100]
Python exception handling (Python learning memo ⑥)
O'Reilly python3 Primer Learning Notes
Learning flow for Python beginners
Python
Effective Python Learning Memorandum Day 12 [12/100]
Python: Supervised Learning: Hyperparameters Part 1
Python learning plan for AI learning
Effective Python Learning Memorandum Day 9 [9/100]
Effective Python Learning Memorandum Day 8 [8/100]
Reinforcement learning starting with Python
Machine learning with Python! Preparation
Effective Python Learning Memorandum Day 1 [1/100]
Python Machine Learning Programming> Keywords
Python: Supervised Learning: Hyperparameters Part 2
Effective Python Learning Memorandum Day 13 [13/100]
Effective Python Learning Memorandum Day 3 [3/100]
Effective Python Learning Memorandum Day 5 [5/100]
Checkio's recommendation for learning Python
Effective Python Learning Memorandum Day 4 [4/100]
Beginning with Python machine learning
Python Iteration Learning with Cheminformatics
Effective Python Learning Memorandum Day 7 [7/100]
Effective Python Learning Memorandum Day 2 [2/100]
Python: Unsupervised Learning: Non-hierarchical clustering
Python control syntax, functions (Python learning memo ②)
Implement stacking learning in Python [Kaggle]
Python + Unity Reinforcement learning environment construction
Python: Application of supervised learning (regression)
Machine learning with python (1) Overall classification
Machine learning summary by Python beginners
Input / output with Python (Python learning memo ⑤)
Notation I encountered while learning Python
Widrow-Hoff learning rules implemented in Python
Perceptron learning experiment learned with Python