python中字符串的fommat方法

Posted NoteBlogSgj

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python中字符串的fommat方法相关的知识,希望对你有一定的参考价值。

 

python 比较python中各种字符串连接方法的性能

from functools import wraps
import pickle
from random import randint, choice
import string
import pathlib
import time
import sys

big_list = []  # our huge list of tuples
cache_file = "test-data-cache.pickle"


def chrono(msg='', format_time=False):
    def decorator(func):
        @wraps(func)
        def wrapper(*args, **kwargs):
            local_chrono = Chrono()
            res = func(*args, **kwargs)
            local_chrono.stop(msg, format_time)
            return res

        return wrapper

    return decorator


class Chrono(object):
    def __init__(self):
        self._start_time = time.perf_counter()

    @staticmethod
    def _format_time(t) -> str:
        hh = t // 3600
        mm = (t % 3600) // 60
        ss = t - (hh * 3600) - (mm * 60)
        return "%02dh%02dm%02ds" % (hh, mm, ss)

    def stop(self, msg='', format_time=False) -> float:
        stop_time = time.perf_counter()
        t_elapsed = stop_time - self._start_time

        if msg:  # print message if any
            if format_time:
                t_str = self._format_time(t_elapsed)
            else:
                t_str = "%.3f seconds" % t_elapsed
            print("[ elapsed %s ] - %s" % (t_str, msg), file=sys.stderr)

        return t_elapsed


def rand_str():
    allchar = string.ascii_letters + string.digits
    return "".join(choice(allchar) for _ in range(randint(6, 18)))


def build_list(n):
    global big_list

    c = Chrono()

    for i in range(n):
        tpl = (
            rand_str(), str(randint(0, 655359999)), str(randint(0, 65535999)), str(randint(0, 655359999)), rand_str())
        big_list.append(tpl + tpl + tpl + tpl)  # each records has 20 fields

    c.stop(f"built list of {list_size} items")

    # save for later
    dumpfile = open(cache_file, 'wb')
    c = Chrono()
    pickle.dump(big_list, dumpfile)
    c.stop("dump cache file")
    dumpfile.close()


def load_list():
    global big_list
    dumpfile = open(cache_file, 'rb')
    c = Chrono()
    big_list = pickle.load(dumpfile)
    c.stop(f"load cache file : {len(big_list)} records")
    dumpfile.close()


@chrono('str.join()')
def test_join():
    arr = []
    for item in big_list:
        s = "|".join(item)
        arr.append(s)
    return arr[-1]


@chrono("percent formatting")
def test_percent_format():
    arr = []
    for item in big_list:
        s = "%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s" % (
            item[0], item[1], item[2], item[3], item[4], item[5], item[6], item[7], item[8], item[9], item[10],
            item[11], item[12], item[13], item[14], item[15], item[16], item[17], item[18], item[19])
        arr.append(s)
    return arr[-1]


@chrono("f-string formatting")
def test_fstring_format():
    arr = []
    for item in big_list:
        s = f"{item[0]}|{item[1]}|{item[2]}|{item[3]}|{item[4]}|{item[5]}|{item[6]}|{item[7]}|" \
            f"{item[8]}|{item[9]}|{item[10]}|{item[11]}|{item[12]}|{item[13]}|{item[14]}|{item[15]}|" \
            f"{item[16]}|{item[17]}|{item[18]}|{item[19]}"
        arr.append(s)
    return arr[-1]


@chrono("multiple string concat (+)")
def test_string_concat():
    arr = []
    for item in big_list:
        s = item[0] + "|" + item[1] + "|" + item[2] + "|" + item[3] + "|" + item[4] + "|" + item[5] + "|" + item[
            6] + "|" + item[7] + "|" + item[8] + "|" + item[9] + "|" + item[10] + "|" + item[11] + "|" + item[
                12] + "|" + item[13] + "|" + item[14] + "|" + item[15] + "|" + item[16] + "|" + item[17] + "|" + item[
                18] + "|" + item[19]
        arr.append(s)
    return arr[-1]


@chrono("str.format()")
def test_format_function():
    arr = []
    for item in big_list:
        s = "{0}|{1}|{2}|{3}|{4}|{5}|{6}|{7}|{8}|{9}|{10}|{11}|{12}|{13}|{14}|{15}|{16}|{17}|{18}|{19}".format(
            item[0], item[1], item[2], item[3], item[4], item[5], item[6], item[7], item[8], item[9], item[10],
            item[11], item[12], item[13], item[14], item[15], item[16], item[17], item[18], item[19])
        arr.append(s)
    return arr[-1]


if __name__ == '__main__':

    p = pathlib.Path(cache_file)
    list_size = 500000

    if p.exists():
        load_list()
        if not len(big_list) == list_size:
            print("... rebuliding the list")
            big_list = []
            build_list(list_size)
    else:
        build_list(list_size)

    v1 = test_join()
    v2 = test_percent_format()
    v3 = test_fstring_format()
    v4 = test_string_concat()
    v5 = test_format_function()

    success = v1 == v2 == v3 == v4 == v5
    if success:
        print("Success: All functions generated the same results")
        exit(0)
    else:
        print("Error: Output results are not the same:")
        [print(v) for v in (v1, v2, v3, v4, v5)]
        exit(1)

以上是关于python中字符串的fommat方法的主要内容,如果未能解决你的问题,请参考以下文章

python3 如何去除字符串中不想要的字符

Python中去除字符串中空格的方法

python中字符串的各种方法

python中进行字符串排序

python 字符串提取信息方法总结

关于python中strip方法的理解