python函数库

字符串
截取字符串

截取指定两个符号间的字符串返回所有符合结果的list

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
def get_list_hash(template):  # HashTag以‘#’开头,以空格或回车结尾
copy = False
finished = False
slotList = []
str = ""
for s in template:
if s == '#':
copy = True
elif s == ' ':
copy = False
finished = True
elif s == '\n':
copy = False
finished = True
elif copy:
str = str + s
if finished:
if str != "":
slotList.append(str)
str = ""
finished = False
return slotList

1
2
3
4
def get_text(template):
rule = r':::(.*?)\n'
slot_list = re.findall(rule, template)
return slot_list
判断是否为英文字母
1
2
3
4
5
6
7
#解决.isalpha()方法对(unicode string,string.isalpha会根据字符串中的字符是否属于Unicode编码的LETTER区域来判断是否都由字母组成。
# 所以得出的结果为True,不一定表示只有26个英文字母。)
def is_alpha(word):
try:
return word.encode('ascii').isalpha()
except UnicodeEncodeError:
return False
文件操作
创建文件夹
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
def mkdir(path):
# 去除首位空格
path = path.strip()
# 去除尾部 \ 符号
path = path.rstrip("/")

# 判断路径是否存在
# 存在 True
# 不存在 False
isExists = os.path.exists(path)

# 判断结果
if not isExists:
# 如果不存在则创建目录
# 创建目录操作函数
os.makedirs(path)

print(str(path) + ' 创建成功')
return True
else:
# 如果目录存在则不创建,并提示目录已存在
print(str(path) + ' 目录已存在')
return False
读取文件

读取并按行遍历文件

1
2
3
4
5
6
7
f_video_text = open("video_text.txt",encoding='utf8', errors='ignore')
while 1:
line_video_text = f_video_text.readline()
if not line_video_text:
break
else:
do something

读取文件夹下所有文件名,返回list
1
2
3
4
def get_list_file_name(file_dir):  # 获取视频名list
for root, dirs, files in os.walk(file_dir):
continue
return files
获取文件行数
1
2
3
4
5
6
7
8
9
10
def get_lines(file_name):
count = 0
thefile = open(file_name, encoding='utf8', errors='ignore')
while True:
buffer = thefile.read(1024 * 8192)
if not buffer:
break
count += buffer.count('\n')
thefile.close()
return count
读取文件dict or list
1
2
3
4
5
6
def get_list_or_dict(path):
f = open(path,encoding='utf8', errors='ignore')
a = f.read()
dict_or_list = eval(a)
f.close()
return dict_or_list
txt文件数据写成dict or list文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
def write_list_or_dict(path_txt,path_dict_or_list_txt):
f = open(path_txt,encoding='utf8', errors='ignore')
while 1:
line_txt = f.readline()
if not line_txt:
break
else:
handle
list_or_dict[id] = content
f.close()
f = open(path_dict_or_list_txt, 'w' , encoding='utf8' , errors='ignore')
f.write(str(list_or_dict))
f.close
print('写入成功')
获取视频或音频时长

获取视频或音频时长,需要安装ffmpeg

1
2
3
4
5
6
7
8
def get_time(filename):
command = 'ffprobe -loglevel quiet -print_format json -show_format -show_streams -i ' + filename
result = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
out = result.stdout.read()

temp = str(out.decode('utf-8'))
data = json.loads(temp)["format"]['duration']
return data