Skip to main content

Python #

(Maybe) Useful resources #

Dictionary #

Append #

new_dict = {}
new_dict.update({"name": number})

Get head of a dictionary: (credit)

from itertools import islice

dict(islice(my_dict.items(), 0, 5))

Help #

Like R’s ? function.


# If it is a method, need something that the method can run from:

List #

Find the index of minimum in a list: (credit)

val, index = min((val, index) for (index, val) in enumerate(my_list))

Write a list to file: (credit)

import os

my_list = ["A", "B", "C"]

# open file in write mode
with open(os.path.join("data", "log.txt"), 'w') as f:
    for item in my_list:
        # write each item on a new line
        f.write("%s\n" % item)


Benchmarking #

Ref: performance - How to measure elapsed time in Python? - Stack Overflow

import time

# use round() if only the first digit is necessary
start = time.time()

# (some heavy load operations)

end = time.time()
elapsed = end - start


This section focuses on the Python module csv. For the package pandas, see Pandas.

Docs: csv — CSV File Reading and Writing — Python 3.11.4 documentation

Import as list #

Ref: Python import csv to list - Stack Overflow

import csv

with open("data.csv", newline="") as f:
    reader = csv.reader(f)
    data = list(reader)

# [
#   ["first", "row"], ["second", "row"], …
# ]


Split a big JSON file into smaller files: (credit)

import os
import json
from itertools import islice

def split_json(
    """Split a big JSON file into chunks.
    file_name : exclude ".json"
    with open(os.path.join(data_path, file_name + ".json"), "r") as f:
        whole_file = json.load(f)

    split = len(whole_file) // size_split

    for i in range(split + 1):
        with open(os.path.join(data_path, file_name + "_"+ str(split+1) + "_" + str(i+1) + ".json"), 'w') as f:
            json.dump(dict(islice(whole_file.items(), i*size_split, (i+1)*size_split)), f)

Merge said smaller files into one file: (my answer)

json_all = dict()
split = 4         # this is the 1-based actual number of splits

for i in range(1, split+1):
    with open(os.path.join("data_folder", "data_file_" + str(split) + "_" + str(i) + ".json"), 'r') as f:
        json_i = json.load(f)

Objects #

List all attributes (& methods) of an object: (credit)

for attr in dir(object_a):
    if not attr.startswith('_'):

List all methods of an object: (credit)

for method in dir(object_a):
    if callable(getattr(object_a, method)) and (not method.startswith('_')):

Pickle #

See Writing functions #Dark magic for the wrapper function version.

import os
import pickle

pickle_path = os.path.join("data", "data.pickle") # path: ./data/data.pickle

# create pickle
with open(pickle_path, "wb") as f:
    pickle.dump(df, f)

# use pickle
if os.path.isfile(pickle_path):
    with open(pickle_path, "rb") as f:
        df = pickle.load(f)
    print("Pickle not found. Please create one first.")

Print #

Bold #

Ref: How can I print bold text in Python? - Stack Overflow

print("\033[1m" + "hello world" + "\033[0m")

Pretty print #

Table #

Ref: python - Printing Lists as Tabular Data - Stack Overflow



Pretty print: (credit)

import json

print(json.dumps(json.loads(json_data), indent=4))

# if get TypeError: the JSON object must be str, bytes or bytearray, not dict
# then use:
print(json.dumps(json_data, indent=4))

Try except #

    assert(len(list) == 5)
except AssertionError:
    print("Error: List length is wrong.")
    print("Error: Unknown error.")