Skip to content

Instantly share code, notes, and snippets.

@yeiichi
yeiichi / txt_to_utf8.py
Created May 30, 2019 06:29
Detects encoding of a txt or csv file and converts it to utf-8.
#!/usr/bin/env python
#_*_ coding: utf-8 _*
import chardet
try:
input_file_name = input('File name?(txt or csv) ')
with open(input_file_name,'rb') as f_bin:
detected_encoding = chardet.detect(f_bin.read())
print(' Detected: '+detected_encoding['encoding'])
@yeiichi
yeiichi / comb_ncr.py
Created May 29, 2020 00:05
Calculates a combination C(n, r)
# https://stackoverflow.com/questions/4941753/
import math
def ncr(n, r):
'''Returns a combination C(n, r)'''
f = math.factorial
return int(f(n) / f(n-r) / f(r))
#!/usr/bin/env python
# coding: utf-8
import os
# Directory set up
my_project_root_path = input('PROJECT_ROOT_PATH = ? >> ')
PROJECT_ROOT_PATH = my_project_root_path
SOURCE_PATH = os.path.join(PROJECT_ROOT_PATH, 'source')
@yeiichi
yeiichi / page_fetcher.py
Last active July 3, 2020 00:32
Fetch a page data using User Agent information.
# Version 1.0.1
# 2020-07-03
import requests
import random
from bs4 import BeautifulSoup
# User agent definition:
# You can check your User Agent at ifconfig.me
UA_LIST = {
'SAFARI': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) \
@yeiichi
yeiichi / week_of_year.py
Last active July 19, 2020 05:27
Memo on 'week of year'
from datetime import datetime
import pandas as pd
# Preparation of a data frame.
date_list = pd.date_range(datetime.now().date(), periods=14)
df = pd.DataFrame(date_list, columns=['DATE'])
day_dict = {'0':'Sun', '1':'Mon', '2':'Tue', '3':'Wed',
'4':'Thu', '5':'Fri', '6':'Sat'}
df['DAY'] = df.DATE.dt.strftime('%w')
@yeiichi
yeiichi / json2csv.py
Created July 24, 2020 05:04
Convert a json dict to a flat csv.
import json
import pandas as pd
def json2csv(file_path):
with open(file_path) as f:
my_dict = json.load(f)
df = pd.DataFrame.from_dict(my_dict, orient='index')
df.to_csv(file_path+'.csv')
print('DONE!')
@yeiichi
yeiichi / soup_simmer.py
Created July 30, 2020 06:28
Beautiful Soup simmer pot
# Page fetcher part:
import requests
from bs4 import BeautifulSoup
import random
# User agent definition:
# You can check your User Agent at ifconfig.me
UA_LIST = {
'SAFARI': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) \
AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.1 Safari/605.1.15',
'FIREFOX': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:76.0) \
@yeiichi
yeiichi / google_fetcher.py
Last active July 31, 2020 08:05
Download a Google Results page.
import os
from urllib.parse import quote_plus, urlunsplit
import requests
import re
PROJECT_ROOT_PATH = '.'
class GoogleResultsPage:
'''Query text, Results number per page -> search results response'''
def __init__(self, query, rslts_num):
@yeiichi
yeiichi / my_chardet.py
Last active August 1, 2020 03:35
my chardet
import chardet
with open(file_path, 'rb') as f:
encoding = chardet.detect(f.read()).get('encoding')
/* CREATE */
CREATE DATABASE IF NOT EXISTS db_name
CHARACTER SET utf8mb4
COLLATE utf8mb4_0900_ai_ci;
-- https://dev.mysql.com/doc/refman/8.0/en/charset-mysql.html
CREATE TABLE IF NOT EXISTS tbl_name (
id SERIAL,
col_name FLOAT(length),