字符串
截取字符串
截取指定两个符号间的字符串返回所有符合结果的list1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22def 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 | def get_text(template): |
判断是否为英文字母
1 | #解决.isalpha()方法对(unicode string,string.isalpha会根据字符串中的字符是否属于Unicode编码的LETTER区域来判断是否都由字母组成。 |
文件操作
创建文件夹
1 | def mkdir(path): |
读取文件
读取并按行遍历文件1
2
3
4
5
6
7f_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 | def get_list_file_name(file_dir): # 获取视频名list |
获取文件行数
1 | def get_lines(file_name): |
读取文件dict or list
1 | def get_list_or_dict(path): |
txt文件数据写成dict or list文件
1 | def write_list_or_dict(path_txt,path_dict_or_list_txt): |
获取视频或音频时长
获取视频或音频时长,需要安装ffmpeg1
2
3
4
5
6
7
8def 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