Python CSV file reading and writing

background

There are many ways to read and write CSV files on the net, It's hard for me to google every time, so I make a memorandum. You can use the pandas library, I think it can be used when the format of the csv file itself is not in order. Once the format is in place, it's easier to use pandas.

Read CSV file

read_csv_file.py


import csv
#csv file
csv_file = "demo.csv"
with open(csv_file) as f:
    reader = csv.reader(f)
    #List of data for each line_Put in
    list_ = [num for num in reader]
#output
print("list_ ", list_ )

Write CSV file

read_csv_file.py


import csv
#csv file
csv_file = "demo.csv"
#newline=""Prevents line breaks when writing
with open(csv_file , 'w', newline="") as f:
    writer = csv.writer(f)
    #Use two types of writerow writerows properly
    writer.writerow(list_1)#list_1[A,B,C,D]
    writer.writerows(list_2)#list_2[[],[],[]]

Recommended Posts

Python CSV file reading and writing
Reading and writing CSV with Python
Reading and writing csv files
Reading and writing CSV and JSON files in Python
Example of reading and writing CSV with Python
[Introduction for beginners] Reading and writing Python CSV files
Reading and writing NetCDF with Python
Reading and writing text in Python
Reading and writing JSON files with Python
[pandas] .csv file reading and display method
[Note] File reading ~ Python ~
Read Python csv file
python external file reading
[Python] Reading CSV files
Study from Python Reading and writing Hour9 files
Writing logs to CSV file (Python, C language)
Reading and writing fits files with Python (memo)
[Python] Reading and writing photo location information tags (JPG file GPS Exif)
Read and write csv file
AHC task (1) Reading CSV file
Download csv file with python
Character code for reading and writing csv files with python ~ windows environment ver ~
[Python] Read the csv file and display the figure with matplotlib
Output to csv file with Python
Python memo ① Folder and file operations
Notes on reading and writing float32 TIFF images in python
uproot: Python / Numpy based library for reading and writing ROOT files
[Python] How to convert db file to csv
Read CSV file with python (Download & parse CSV file)
[Python] Convert csv file delimiters to tab delimiters
Copy file and rewrite cell value @python
(Python) HTML reading and regular expression notes
Read Python csv and export to txt
See file and folder information on python
[python] Read html file and practice scraping
Script python file
Read and format a csv file mixed with comma tabs with Python pandas
Python file processing
Basic Python writing
Python CSV file Character code conversion, file name extraction, reading, output, merging operation
Read the file with python and delete the line breaks [Notes on reading the file]
Csv in python
Read CSV file with Python and convert it to DataFrame as it is
Error due to UnicodeDecodeError when reading CSV file with Python [For beginners]
Raspberry Pi 3 x Julius (reading file and grammar file)
Scraping tabelog with python and outputting to CSV
File DL, byte value and delete in Python3
Read JSON with Python and output as CSV
CRLF becomes LF when reading a Python file
Python hand play (interoperability between CSV and PostgreSQL)
Output python log to both console and file
Transpose CSV file in Python Part 2: Performance measurement
Comparison of R and Python writing (Euclidean algorithm)
Case sensitive when reading and writing INI files
Speed evaluation of CSV file output in Python
A Python script that reads a SQL file, executes BigQuery and saves the csv
[python] Compress and decompress
[Python] AGC043A (Problem reading comprehension and DP) [At Coder]
[Python] File / directory operations
Reading CSV data from DSX object storage Python code
Full-width and half-width processing of CSV data in Python