[Python] Frequently used library code

Use json as config file


import json

settings = json.load(open('settings.json', encoding='utf8'))


    "key1" : "value1",
    "key2" : "value2"

txt file operation

Use txt file as input data (store in array)


with open('file.txt', mode='r', encoding='utf-8') as f:
    file = f.read()

files = file.split('\n')

Use a txt file as output data (used in a for statement)


with open(filepath, mode='a', encoding='utf-8') as f:

File operations

Copy file


import shutil

shutil.copy2(path_src, path_dist)

Get date


import datetime

now = datetime.datetime.now()
now_disp = dt_now.strftime('%Y%m%d')

Get a list of files in a directory (array)


file_list = os.listdir(path=filepath)

#An excerpt of a file containing a specific character from the array of files obtained above
file_list_unique = [s for s in file_list if 'Specified character string' in s]

web scraping


pip install requests bs4


import requests
from bs4 import BeautifulSoup

url = 'website.com'
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
# soup = BeautifulSoup(response.text, 'lxml')
# soup = BeautifulSoup(response.text, 'html5lib')

#Single acquisition
print(soup.select_one('.css-selector').text) #Get a string
print(soup.select_one('.css-selector').attrs['href']) #Get the value of an attribute

#If there are more than one`select`Get it with and turn it with for
htmls = soup.select('.css-selector')
for html in htmls:

Excel operation


pip install openpyxl


import openpyxl

wb = openpyxl.load_workbook(filepath)
ws = wb['Sheet name']

#Get the value of a specific cell (A1 is row=1, column=1)
cell_obj = ws.cell(row=1, column=1).value

#Enter a value in a specific cell
ws.cell(row=1, column=1).value = 'value'

#Get all the values ​​in a specific column (* Note: 0 is column A in the array index)
for cell_obj in list(ws.columns)[0]:

#Get all the values ​​in a specific row (* Note: 0 is column A in the array index)
for cell_obj in list(ws.rows)[0]:

#Sheet save

Browser automatic operation

Download chromedriver and place it directly under the project https://chromedriver.chromium.org/downloads


pip install selenium pyperclip


from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.keys import Keys
import pyperclip

#Chrome Driver settings
driver_path = './chromedriver'
options = Options()
driver = webdriver.Chrome(options=options, executable_path=driver_path)


#Chrome Driver operation
##Show argument URL

##Click the specified selector

##Character input (send)_Since keys is slow, copy the character string with pyperclip and paste it)
pyperclip.copy('The character string you want to enter')
driver.find_element_by_css_selector('.css-selector').send_keys(Keys.CONTROL, 'v')

##Javascript execution (Example: Set the display property of the specified selector to none)

##Quit Chrome Driver

Make it an executable file


pip install pyinstaller


pyinstaller filename.py

pyinstaller filename.py --onefile #One.Put together in an exe file
pyinstaller filename.py --noconsole # .Do not show console when running exe
pyinstaller filename.py --onefile --noconsole #Both of the above

Recommended Posts

[Python] Frequently used library code
Python frequently used code snippets
8 Frequently Used Commands in Python Django
Python 3.6 email library
Python ast library
Python + Selenium Frequently used operation method summary
[python] Frequently used techniques in machine learning
python character code
Python Library notes
[Python] Algorithm-aware code
Summary of frequently used Python arrays (for myself)
Code often used in Python / Django apps [prefectures]
pyenv Frequently used commands
Frequently used tmux commands
Python code acceleration approach
Frequently used Linux commands
Frequently used Linux commands
Rewrite Python2 code to Python3 (2to3)
infomap python draw code
Before writing Python code
Install python external library
About Python3 character code
Python optimization library Pulp
Python Requests status code
OpenCV basic code (python)
Frequently used pip commands
Note the frequently used options in Python + Selenium + Chrome
A collection of code often used in personal Python
List of Python code used in big data analysis
Can be used in competition pros! Python standard library
Comparison table of frequently used processes of Python and Clojure
[Redash] Standard library cannot be used in python function
Aim python library master (48) autopep8
Aim python library master (36) json2html
Aim python library master (49) psidialogs
Get country code with python
Frequently used ps command options
Aim python library master (26) easyxml
Python with VS Code (Windows 10)
Aim python library master (29) table_printer
Aim python library master (55) namespaces
Aim python library master (46) browserplus
Python Evolutionary Computation Library Deap (3)
Aim python library master (30) chronyk
Aim python library master (3) workalendar
Aim python library master (42) speedrecorder
Aim python library master (37) slimurl
Recommendation of binpacking library of python
Aim python library master (44) pynetviz
Aim python library master (8) rolex
How Python __dict__ is used
Aim python library master (52) marktime
Aim python library master (21) hy
Windows10: Install python dlib library
Aim python library master (13) easydev
Aim python library master (20) pyyaml
Aim python library master (34) concurrent
Aim python library master (40) wordsegmentation
Aim python library master (43) cpmoptimize
Aim python library master (58) faker
Aim python library master (11) nlist