一、从一堆文本信息中提取出手机号码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#读取目标文本文件
def get_str(path):
f = open(path,encoding="utf-8")
data = f.read()
f.close()
return data

import re

#正则获取文本号码
def get_phone_number(str):
res = re.findall(r'(13\d{9}|14[5|7]\d{8}|15\d{9}|166{\d{8}|17[3|6|7]{\d{8}|18\d{9})', str)
return res

#保存得到号码
def save_res(res,save_path):
save_file = open(save_path, 'w')
for phone in res:
save_file.write(phone)
save_file.write('\n')
save_file.write('\n号码共计:'+str(len(res)))
save_file.close()
print('号码读取OK,号码共计:'+str(len(res)))

path=input("请输入文件路径:")
save_path=input("请输入文件保存路径:")
#read_str=get_str(path)
res=get_phone_number(get_str(path))
save_res(res,save_path)

二、电话号码存储到 Excel 中

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import xlwt
#读取目标文本文件
def get_str(path):
f = open(path,encoding="utf-8")
data = f.read()
f.close()
return data
def save_excel(save_path,sheetname,column_name_list,read_list):
workbook = xlwt.Workbook()
sheet1 = workbook.add_sheet(sheetname=sheetname)
for i in range(0,len(column_name_list)):
sheet1.write(0,i,column_name_list[i])
i=1
for v in read_list:
kval=v.split(':')
for j in range(0,len(kval)):
sheet1.write(i+1,j,kval[j])
print(kval[j])
i=i+1


#保存为Excel文件
def save_excel(save_path,sheetname,column_name_list,read_list):
workbook = xlwt.Workbook()
sheet1 = workbook.add_sheet(sheetname=sheetname)
for i in range(0,len(column_name_list)):
sheet1.write(0,i,column_name_list[i])
i=1
for v in read_list:
kval=v.split(':')
for j in range(0,len(kval)):
sheet1.write(i+1,j,kval[j])
i=i+1
workbook.save(save_path)
print('信息保存 OK,记录条数共计:'+str(len(read_list)))

path=input("请输入文件路径:")
save_path=input("请输入文件保存路径:")
sheet_name=input("请输入sheetname:")
column_name=input("请输入列名,并且使用英文逗号隔开:")
column_name_list=column_name.split(',')

read_str=get_str(path)
read_list=read_str.split('\n')
save_excel(save_path,sheet_name,column_name_list,read_list)

电话号码存储到 Excel 中

三、从文本中提取邮箱

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import re

#正则获取目标信息
def get_re_str(str):
res = re.findall(r'^[A-Za-z0-9\u4e00-\u9fa5]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$', str)
return res

#读取目标文本文件
def get_str(path):
f = open(path,encoding="utf-8")
data = f.read()
f.close()
return data

#保存得到的信息
def save_res(res,save_path):
save_file = open(save_path, 'w')
for phone in res:
save_file.write(phone)
save_file.write('\n')
save_file.close()
print('信息读取OK,信息共计:'+str(len(res)))

path=input("请输入文件路径:")
save_path=input("请输入文件保存路径:")
#read_str=get_str(path)
res=get_re_str(get_str(path))
save_res(res,save_path)

电话号码存储到 Excel 中

四、给图片加水印

1
2
3
4
5
6
7
8
import cv2 
import os
path=input("请输入需要加水印的文件夹路径:")
file_list = os.listdir(path)
for filename in file_list:
img1 = cv2.imread(path+filename,cv2.IMREAD_COLOR)
cv2.putText(img1,'CSDN',(10,10) , 1, 1, (255,255,255),1) #图片,文字,位置,字体,字号,颜色,厚度
cv2.imwrite(path+filename, img1)

电话号码存储到 Excel 中

五、清理重复文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import hashlib,os
def getMD5(filepath):
f = open(filepath,'rb')
md5obj = hashlib.md5()
md5obj.update(f.read())
hash = md5obj.hexdigest()
f.close()
return str(hash).upper()
path=input("请输入需要重复文件过滤文件夹路径:")
file_list = os.listdir(path)
file_md5=[]
for filename in file_list:
md5val=getMD5(path+filename)
if md5val in file_md5:
os.remove(path+filename)
else:
file_md5.append(md5val)
print("处理完毕...")

电话号码存储到 Excel 中

六、统计文本到底有多少个中文字符

1
2
3
4
5
6
7
8
9
10
11
#读取目标文本文件
def get_str(path):
f = open(path)
data = f.read()
f.close()
return data
path=input("请输入文件路径:")
path=input("请输入文件路径:")

word=re.findall('([\u4e00-\u9fa5])',get_str(path))
print("中文字符,除特殊字符外共:",len(word))

电话号码存储到 Excel 中

七、将网址生成二维码图片

1
2
3
4
5
6
7
8
9
10
import qrcode
qr = qrcode.QRCode(
version=2,#尺寸
error_correction=qrcode.constants.ERROR_CORRECT_L,#容错信息当前为 7% 容错
box_size=10,#每个格子的像素大小
border=1#边框格子宽度
)#设置二维码的大小
qr.add_data("https://www.csdn.net/")#指定 url
img = qr.make_image()#生成二维码图片
img.save("F:\work\day7\csdn.png")#保存

电话号码存储到 Excel 中

八、将图片生成 gif

1
2
3
4
5
6
7
import imageio
image_list = [r'F:\work\day4\1.png', r'F:\work\day4\2.png']
gif_name = r'F:\work\day4\gif.gif'
frames = []
for image_name in image_list:
frames.append(imageio.imread(image_name))
imageio.mimsave(gif_name, frames, 'GIF', duration=2)

电话号码存储到 Excel 中

九、翻译一份英文文档

1
2
3
4
5
6
7
8
9
10
11
12
from translate import Translator
translator = Translator(to_lang="Chinese")
def get_str(path):
f = open(path)
data = f.read()
f.close()
return data
path=input("请输入文件路径:")
text=get_str(path)
Virtual Group Coaching: How to Improve Group Relationships in Remote Work Settings
translation = translator.translate(text)
print(translation)

电话号码存储到 Excel 中

十、提取视频的音频信息

1
2
3
from moviepy.editor import AudioFileClip
my_audio_clip = AudioFileClip("E:\PyVedio\py02.mp4")
my_audio_clip.write_audiofile("E:\PyVedio\py02.wav")

电话号码存储到 Excel 中

https://mp.weixin.qq.com/s/Uy8xXpQrqXlYrUSwFNAnnQ