python : case

Variable parameter function
Delete blanklines of infile
#!/usr/bin/python3
#!/usr/bin/env python3

/# -*- coding: utf-8 -*-

'variable parameter 可变参数函数'
def arithmetic_mean(*args):
    sum = 0
    for x in args:
        sum += x
    return sum
#print(arithmetic_mean(49,50,51)) #150

'Delete blanklines of infile '
def delblankline(infile, outfile):
    infp = open(infile, "r")
    outfp = open(outfile, "w")
    lines = infp.readlines()
    for li in lines:
        if li.split():
            outfp.writelines(li)
    infp.close()
    outfp.close()
Using Python to generate 200 activation code (or coupon)
import random

'filename:存放激活码的文件, digit:每个激活码的位数, num:生成激活码的个数'
def randChar(filename, digit=4, num=200):
    f = open(filename, 'a')
    for i in range(0, num):     
        for m in range(0, digit):
            f.write(chr(random.randint(65, 90)))
        f.write('\n')
    f.close()
    print('Done!')
    return 0

if __name__ == '__main__':
    filename = 'active_code.txt'
    digit = 5
    num = 100
    randChar(filename, digit, num)
English either plain text file, count the number of words which appear
import os
import re
import glob
from collections import OrderedDict

'获得词汇出现次数'
def get_num(key_word, filename):
    f = open(filename, 'r', encoding='utf-8').read()
    re_zhengze = re.compile(r'[\s\,\;\.\n]{1}'+key_word+r'[\s\,\;\.\n]{1}')
    numbers = re_zhengze.findall(f)
    return len(numbers)

def article_analysis(dirs):
    article = glob.glob(r'*.txt')
    dictdata = OrderedDict()
    for m in article:
        doc = open(m, 'r', encoding='utf-8').read()
        doc = re.findall(r'[\w\-\_\.\']+', doc)                 #获得单词list
        doc = list(map(lambda x: x.strip('.'), doc))            #去除句号
        for n in doc:
            dictdata[n] = get_num(n, m)
        a = OrderedDict(sorted(dictdata.items(), key=lambda x: x[1], reverse = True))   #dict排序
        print('在 %s 中出现次数最多的单词是:' % m)
        for c in a:
            print(c+' : %s 次' % a[c])
            break
    return 0
Sensitive word text file filtered_words.txt, when a user enters sensitive words, then replaced by an asterisk *. For example, when the user enters "Beijing is a good city," turned into "** is a good city."
import os
import re

def filter_word(a):
    sensitive = False
    strs = '**'
    f = open('filtered_words.txt', 'r', encoding = 'utf-8').readlines()
    for i in f: 
        i = i.strip()                                                     #去除\n
        b = re.split(r'%s' % (i), a)                                      #分解字符串
        if len(b) > 1:
            c = i
            sensitive = True
        else:
            pass
    if sensitive == True:
        b = re.split(r'%s' % (c.strip()), a)
        print (b)
        print(strs.join(b))
    else:
        print(a)
    return 0
    
if __name__ == '__main__':
    z = input('请输入:')
    filter_word(z)
Published 25 original articles · won praise 2 · Views 814

Guess you like

Origin blog.csdn.net/yangjinjingbj/article/details/104056738