2024-09-13-python-standard-library2

post

|

python

|

2018-09-13 11:00:24

|

格式化输出

  • reprlib

import reprlib

reprlib.repr(set(‘supercalifragilisticexpialidocious’))

#“{‘a’, ‘c’, ‘d’, ‘e’, ‘f’, ‘g’, …}”

  • pprint

import pprint

t = [[[[‘black’, ‘cyan’], ‘white’, [‘green’, ‘red’]], [[‘magenta’,

‘yellow’], ‘blue’]]]

pprint.pprint(t, width=30)

#[[[[‘black’, ‘cyan’],

‘white’,

[‘green’, ‘red’]],

[[‘magenta’, ‘yellow’],

‘blue’]]]

  • textwrap

import textwrap

doc = “”"The wrap() method is just like fill() except that it returns

a list of strings instead of one big string with newlines to separate

the wrapped lines.“”"

print(textwrap.fill(doc, width=40))

#The wrap() method is just like fill()

#except that it returns a list of strings

#instead of one big string with newlines

#to separate the wrapped lines.

  • locale

import locale

locale.setlocale(locale.LC_ALL, ‘English_United States.1252’)

‘English_United States.1252’

conv = locale.localeconv() # get a mapping of conventions

x = 1234567.8

locale.format(“%d”, x, grouping=True)

#‘1,234,567’

locale.format_string(“%s%.*f”, (conv[‘currency_symbol’],

conv[‘frac_digits’], x), grouping=True)

#‘$1,234,567.80’

多线程

import threading, zipfile

import time

class TestThread(threading.Thread):

def init(self, instring):

threading.Thread.init(self)

self.out=instring

def run(self):

time.sleep(10)

print(self.out)

background = TestThread(‘test string…’)

background.start()

print(‘The main program continues to run in foreground.’)

background.join() # Wait for the background task to finish

print(‘Main program waited until background was done.’)

日志输出

import logging

logging.debug(‘Debugging information’)

logging.info(‘Informational message’)

logging.warning(‘Warning:config file %s not found’, ‘server.conf’)

logging.error(‘Error occurred’)

logging.critical(‘Critical error – shutting down’)

列表操作

  • bisect

import bisect

scores = [‘a’,‘aa’,‘aaa’,‘aaaaa’,‘aaaaaaaaaa’]

bisect.insort(scores, ‘aaaa’)

print(scores)

#[‘a’, ‘aa’, ‘aaa’, ‘aaaa’, ‘aaaaa’, ‘aaaaaaaaaa’]

  • deque

from collections import deque

d = deque([“task1”, “task2”, “task3”])

d.append(“task4”)

print(“Handling”, d.popleft(),d.pop())

#Handling task1 task4

  • array

from array import array

a = array(‘H’, [4000, 10, 700, 22222])

print(sum(a))

print(a[1:3])

#26932

array(‘H’, [10, 700])

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值