Csv tinkering with python

Copy file

copy.py


import shutil
shutil.copy('Copy source path', 'Copy destination path')

Move files

move.py


import shutil
shutil.move('Path before moving', 'Path after moving')

File encoding (utf8 → shift_jis)

encode.py


import codecs
def main():
    # UTF-8 File path
    utf8_csv_path = 'utf8 file path'
    # Shift-jis file path
    shiftjis_csv_path= 'shift-jis file path'

    #Convert character code to shiftjis and save
    fin = codecs.open(utf8_csv_path, "r", "utf-8")
    fout_jis = codecs.open(shiftjis_csv_path, "w", "shift_jis")
    for row in fin:
        fout_jis.write(row)
    fin.close()
    fout_jis.close()
if __name__ == "__main__":
    main()

Delete file

remove.py


import os
os.remove('File path you want to delete')

File name change

renameFile.py


import os
os.rename('File path before change', 'File path after change')

Rename column of csv

renameColumns.py


import pandas as pd

df = pd.read_csv('202101.csv')

print("Before Column\t"+str(df.columns)) #Column name confirmation

df = df.rename(columns = {'A':'aaa', 
                        'B':'bbb', 
                        'C':'ccc', 
                        'D':'ddd', 
                        'E':'eee', 
                        'F':'fff', 
                        'G':'ggg', 
                        'H':'hhh', 
                        'I':'iii', 
                        })

print("After Column\t"+str(df.columns)) #Confirmation of column name after change

df.to_csv('202101_changeColumnName.csv', index = False) #Write changes to csv

Get date (this time YYYYMM)

today = datetime.date.today()
print(today.strftime('%Y%m'))

Recommended Posts

Csv tinkering with python
Read csv with python pandas
Write to csv with Python
Download csv file with python
Make apache log csv with python
[Python] Write to csv file with Python
Output to csv file with Python
Handle Excel CSV files with Python
Reading and writing CSV with Python
FizzBuzz with Python3
Scraping with Python
Statistics with python
Scraping with Python
Python with Go
Twilio with Python
Integrate with Python
Play with 2016-Python
AES256 with python
Tested with Python
python starts with ()
with syntax (Python)
Bingo with python
Zundokokiyoshi with python
Csv in python
Excel with Python
Microcomputer with Python
Cast with python
Read CSV file with python (Download & parse CSV file)
[Part1] Scraping with Python → Organize to csv!
Serial communication with Python
Zip, unzip with python
Django 1.11 started with Python3.6
Primality test with Python
Python with eclipse + PyDev.
Socket communication with Python
Data analysis with python 2
Scraping with Python (preparation)
How to read a CSV file with Python 2/3
Try scraping with Python.
Csv output from Google search with [Python]! 【Easy】
Scraping tabelog with python and outputting to CSV
Learning Python with ChemTHEATER 03
Sequential search with Python
"Object-oriented" learning with python
Run Python with VBA
Handling yaml with python
Solve AtCoder 167 with python
[Python] Use JSON with Python
Learning Python with ChemTHEATER 05-1
Learn Python with ChemTHEATER
Run prepDE.py with python3
1.1 Getting Started with Python
Collecting tweets with Python
Binarization with OpenCV / Python
3. 3. AI programming with Python
Read Python csv data with Pandas ⇒ Graph with Matplotlib
Read JSON with Python and output as CSV
Non-blocking with Python + uWSGI
Scraping with Python + PhantomJS
Posting tweets with python
Drive WebDriver with python