[Python] Frequently used library code

Use json as config file

python


import json

settings = json.load(open('settings.json', encoding='utf8'))
print(settings['key'])

settings.json


{
    "key1" : "value1",
    "key2" : "value2"
}

txt file operation

Use txt file as input data (store in array)

python


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

files = file.split('\n')
print(files)

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

python


with open(filepath, mode='a', encoding='utf-8') as f:
    f.write('String\n')

File operations

Copy file

python


import shutil

shutil.copy2(path_src, path_dist)

Get date

python


import datetime

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

Get a list of files in a directory (array)

python


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

terminal


pip install requests bs4

python


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:
    print(html.select_one('.css-selector').text)

Excel operation

terminal


pip install openpyxl

python


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]:
    print(cell_obj.value)

#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]:
    print(cell_obj.value)

#Sheet save
wb.save(filepath)

Browser automatic operation

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

terminal


pip install selenium pyperclip

python


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()
options.add_argument('--window-size=1600,900')
driver = webdriver.Chrome(options=options, executable_path=driver_path)

IMPLICITLY_INTERVAL = 10
driver.implicitly_wait(IMPLICITLY_INTERVAL)

#Chrome Driver operation
##Show argument URL
driver.get('https://sample.com')

##Click the specified selector
driver.find_element_by_css_selector('.css-selector').click()

##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)
driver.execute_script('document.querySelector(".css-selector").style.display="none";')

##Quit Chrome Driver
driver.quit()

Make it an executable file

terminal


pip install pyinstaller

terminal


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