[Introduction for beginners] Reading and writing Python CSV files

1.First of all

This time, I will describe how to write a CSV file in Python and read the CSV file.

2. Source code

--Write data line by line to the CSV file using a list --When writing as an appendix, specify `a``` in the argument mode``` of ``` open```. --Specify `` " and `in the argument ``` newline``` of open``` --There is no problem because ``` utf_8``` is specified for the character code (`` encoding) at the time of creation, but the characters at the time of reading so that even a file with BOM can be read normally Specify `` `" utf_8_sig " in the code ( `encoding```) ( utf_8_sig``` indicates with UTF-8 BOM) --If the characters are garbled when writing CSV, specify `` utf_8_sig in the character code (` `encoding) when writing.

test.py


import csv

fle = r"C:\Users\USER\Desktop\test.csv"

with open(file=fle, mode="w", encoding="utf_8", newline="") as wf:
    writer = csv.writer(wf)
    writer.writerow(["1", "aaa"])
    writer.writerow(["2", "bbb"])
    writer.writerow(["3", "ccc"])

with open(file=fle, mode="r", encoding="utf_8_sig") as rf:
    lines = csv.reader(rf)
    for line in lines:
        print(line)

Execution result


PS C:\Users\USER> & python c:/Users/USER/Desktop/test.py
['1', 'aaa']
['2', 'bbb']
['3', 'ccc']
PS C:\Users\USER> 

3. Conclusion

I will not describe it in depth.

Recommended Posts

[Introduction for beginners] Reading and writing Python CSV files
Reading and writing csv files
Reading and writing CSV and JSON files in Python
Python CSV file reading and writing
Reading and writing CSV with Python
Character code for reading and writing csv files with python ~ windows environment ver ~
Reading and writing JSON files with Python
[Python] Reading CSV files
uproot: Python / Numpy based library for reading and writing ROOT files
Study from Python Reading and writing Hour9 files
Reading and writing fits files with Python (memo)
Example of reading and writing CSV with Python
Reading and writing NetCDF with Python
Reading and writing text in Python
[Python] Master the reading of csv files. List of main options for pandas.read_csv.
Error due to UnicodeDecodeError when reading CSV file with Python [For beginners]
Causal reasoning and causal search with Python (for beginners)
PDF files and sites useful for learning Python 3
Introduction to Programming (Python) TA Tendency for beginners
Case sensitive when reading and writing INI files
[Introduction for beginners] Working with MySQL in Python
python textbook for beginners
Python Beginner's Guide (Introduction)
OpenCV for Python beginners
Rock-paper-scissors poi in Python for beginners (answers and explanations)
Python text reading for multiple lines and one line
Python # How to check type and type for super beginners
Recursively search for files and directories in Python and output
Notes on writing config files for Python Note: configparser
Learning flow for Python beginners
Python #function 2 for super beginners
Reading .txt files with Python
Basic Python grammar for beginners
100 Pandas knocks for Python beginners
Python for super beginners Python #functions 1
Python #list for super beginners
~ Tips for beginners to Python ③ ~
Introduction to Python For, While
[Introduction to Udemy Python3 + Application] 42. for statement, break statement, and continue statement
How to learn TensorFlow for liberal arts and Python beginners
Python code for writing CSV data to DSX object storage
[Introduction to Python] Combine Nikkei 225 and NY Dow csv data
[Python] Introduction to graph creation using coronavirus data [For beginners]
[Python] Accessing and cropping image pixels using OpenCV (for beginners)
[Python] Until scraping beginners save J-League standings to CSV files
Notes on reading and writing float32 TIFF images in python
An introduction to statistical modeling for data analysis (Midorimoto) reading notes (in Python and Stan)
Transpose CSV files in Python Part 1
[Python] Loading csv files using pandas
Pandas basics for beginners ① Reading & processing
[Introduction to Python3 Day 1] Programming and Python
Python Exercise for Beginners # 2 [for Statement / While Statement]
Minimum grammar notes for writing Python
Python for super beginners Python # dictionary type 1 for super beginners
How to import CSV and TSV files into SQLite with Python
Manipulate files and folders in Python
Read and use Python files from Python
Python #index for super beginners, slices
Hadoop introduction and MapReduce with Python
Notes for me python csv graph
<For beginners> python library <For machine learning>