Python实用技巧

1. Crontab 配置 Python 定时任务

使用 Python 3 写脚本,但是在 Crontab 中配置定时任务,脚本包:UnicodeEncodeError: ‘ascii’ codec can’t encode characters in position 0-1: ordinal not in range(128)
按理说,使用 Python 3 不应该再有这种错误,Python3 已经全部自动使用了 Unicode编码,这种错误是由于编码不一致导致的。具体原因,我怀疑是 Crontab 运行定时任务时,调用的系统语言环境不是 UTF-8 编码导致的。
解决办法:

  1. 设置系统编码;
export LC_ALL="en_US.utf8"
  1. Python脚本中重定向编码;
import sys
import codecs

sys.stdout = codecs.getwriter("utf-8")(sys.stdout.detach())

Reference: https://www.cnblogs.com/lsdb/p/12470739.html

2. 字符编码

推荐使用 Python 3,如果因为项目原因必须,Python 2, 编码遵循如下 3 点:

2.1 文件编码

   import sys
   
   reload(sys)
   sys.setdefaultencoding("UTF-8")

2.2 中文字符串编码

所有字符串都添加 u’’

2.3 字符串的 isinstance

  1. Python 2 中使用:insistance(u’一个字符串’, basestring)
  2. Python 3 中使用:insistance(u’一个字符串’, str)

2.4 查找字符串

查找使用的是 >= 0,而不是 > 0.

`u'一个字符串'`.find(u'一个') >= 0

2.5 判断是否是中文

    def _is_chinese_char(self, cp):
        """Checks whether CP is the codepoint of a CJK character."""
        # This defines a "chinese character" as anything in the CJK Unicode block:
        #     https://en.wikipedia.org/wiki/CJK_Unified_Ideographs_(Unicode_block)
        #
        # Note that the CJK Unicode block is NOT all Japanese and Korean characters,
        # despite its name. The modern Korean Hangul alphabet is a different block,
        # as is Japanese Hiragana and Katakana. Those alphabets are used to write
        # space-separated words, so they are not treated specially and handled
        # like the all of the other languages.
        if ((cp >= 0x4E00 and cp <= 0x9FFF) or  #
            (cp >= 0x3400 and cp <= 0x4DBF) or  #
            (cp >= 0x20000 and cp <= 0x2A6DF) or  #
            (cp >= 0x2A700 and cp <= 0x2B73F) or  #
            (cp >= 0x2B740 and cp <= 0x2B81F) or  #
            (cp >= 0x2B820 and cp <= 0x2CEAF) or
            (cp >= 0xF900 and cp <= 0xFAFF) or  #
            (cp >= 0x2F800 and cp <= 0x2FA1F)):  #
            return True

        return False

# for char in text:
# 	cp = ord(char)
# 	if self._is_chinese_char(cp)

2.6 字符串全角/半角转换

def Q2B(uchar):
    """单个字符 全角转半角"""
    inside_code = ord(uchar)
    if inside_code == 0x3000:
        inside_code = 0x0020
    else:
        inside_code -= 0xfee0
    if inside_code < 0x0020 or inside_code > 0x7e:  # 转完之后不是半角字符返回原来的字符
        return uchar
    return chr(inside_code)


def B2Q(uchar):
    """单个字符 半角转全角"""
    inside_code = ord(uchar)
    if inside_code < 0x0020 or inside_code > 0x7e:  # 不是半角字符就返回原来的字符
        return uchar
    if inside_code == 0x0020:  # 除了空格其他的全角半角的公式为: 半角 = 全角 - 0xfee0
        inside_code = 0x3000
    else:
        inside_code += 0xfee0
    return chr(inside_code)


def Q2B_string(strs):
    """字符串 全角转半角"""
    new_list = list()
    for uchar in strs:
        new_list.append(Q2B(uchar))
    return ''.join(new_list)

2.7 判断是否是标点符号

def _is_punctuation(char):
    """Checks whether `chars` is a punctuation character."""
    cp = ord(char)
    # We treat all non-letter/number ASCII as punctuation.
    # Characters such as "^", "$", and "`" are not in the Unicode
    # Punctuation class but we treat them as punctuation anyways, for
    # consistency.
    if ((cp >= 33 and cp <= 47) or (cp >= 58 and cp <= 64) or
        (cp >= 91 and cp <= 96) or (cp >= 123 and cp <= 126)):
        return True
    cat = unicodedata.category(char)
    if cat.startswith("P"):
        return True
    return False

2.8 去除非间距字符

def _run_strip_accents(self, text):
        """Strips accents from a piece of text."""
        # 这个函数去除掉text中的非间距字符

        # 标准化对于任何需要以一致的方式处理Unicode文本的程序都是非常重要的。
        # 当处理来自用户输入的字符串而你很难去控制编码的时候尤其如此。
        # normalize() 将文本标准化,第一个参数指定字符串标准化的方式,NFD表示字符应该分解为多个组合字符表示
        text = unicodedata.normalize("NFD", text)
        output = []
        for char in text:
            # category() 返回字符在UNICODE里分类的类型
            cat = unicodedata.category(char)
            if cat == "Mn":
                #  Mark, Nonspacing 指示字符是非间距字符,这指示基字符的修改。
                # https://www.fileformat.info/info/unicode/category/Mn/list.htm
                continue
            output.append(char)
        return "".join(output) 

https://www.programcreek.com/python/example/1020/unicodedata.category

2.9 全角转半角

def strQ2B(ustring):
    """全角转半角"""
    res = ""
    for uchar in ustring:
        inside_code = ord(uchar)
        # 全角空格直接转换
        if inside_code == 12288:
            inside_code = 32
        elif 65281 <= inside_code <= 65374:
            # 全角字符(除空格)根据关系转化
            inside_code -= 65248

        res += chr(inside_code)
    return res

2.10 判断是否全部是中文


def is_all_chinese(str_in):
    for i in str_in:
        if not '\u4e00' <= i <= '\u9fa5':
            return False
    return True

2.11 判断是否全部是英文

def is_all_eng(str_in):
    for i in str_in:
        if i not in string.ascii_lowercase + string.ascii_uppercase:
            return False
    return True

2.12 删除括号

def trim_parent_bracket(str_in):
    res = re.findall(r'[(](.*?)[)]', str_in)
    for ele in res:
        if is_all_eng(ele):
            str_in = str_in.replace('(' + ele + ')', '')
    return str_in
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python有很多实用的技巧,以下是其中几个: 1. 三元运算符:三元运算符是一种简洁的条件语句,可以在一行代码中实现类似于if-else语句的功能。它的语法是 condition_if_true if condition else condition_if_false。这可以大大减少代码的冗余,并增加可读性。 2. 列表推导式:列表推导式是Python中一种快速创建列表的方法。它使用一种简洁的语法,在一个表达式中生成一个新的列表。例如,你可以使用列表推导式快速生成一个包含一系列数字的列表,而不需要使用循环语句。 3. 上下文管理器:上下文管理器是一种在代码块执行之前和之后执行特定代码的方法。它可以用于处理资源的分配和释放,如打开和关闭文件或数据库连接。使用with语句,可以确保资源在使用完毕后被正确释放,避免资源泄漏。 4. Lambda函数:Lambda函数是一种匿名函数,它可以在一行代码中定义简单的函数。它非常适合于需要临时定义函数的情况,可以作为参数传递给其他函数。Lambda函数可以提高代码的可读性并减少代码的数量。 5. 错误处理与异常处理:Python提供了异常处理机制,可以捕捉和处理程序中可能出现的错误。通过使用try-except语句,可以捕获异常并执行特定的代码来处理异常情况,以避免程序崩溃。这对于保证程序的稳定性和可靠性非常重要。 这些实用技巧可以帮助开发人员更高效地编写Python代码,提高代码的可读性和可维护性。通过学习和应用这些技巧,你可以节省时间和精力,并使你的代码更加优雅和高效。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [16个好用到爆的Python实用技巧!](https://blog.csdn.net/ekcchina/article/details/131041778)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [30个Python常用技巧,10分钟get让你的python技术更上一层楼!](https://blog.csdn.net/Python_2332/article/details/130779200)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值