from allpairspy import AllPairs
import re
def fread(file_path):
f = open(file_path, "r",encoding='utf-8')
data = f.readlines()
f.close()
return data
def parse(data):
parameters = []
for line in data:
a = line.strip('\n')
b = a.replace(' ', '')
c = b[b.rfind(':', 1) + 1:]
d = c[c.rfind(':', 1) + 1:]
e = re.split('[,,]', d)
parameters.append(e)
return parameters
def allpairs(parameters,newfile_path):
with open(newfile_path,"w",encoding='utf-8') as f2:
f2.write("PAIRWISE:\n")
for i, pairs in enumerate(AllPairs(parameters)):
f2.write(f"{i+1:2d}:{pairs}\n")
if __name__ == "__main__":
file_path = r'..\data\data1.txt'
parameters = parse(fread(file_path))
newfile_path = r'..\data\newdata1.txt'
allpairs(parameters,newfile_path)
姓名:张三,李四,王五
性别:男,女
年龄:20,30,40
学位:学士,硕士,博士
PAIRWISE:
1:['张三', '男', '20', '学士']
2:['李四', '女', '30', '学士']
3:['王五', '女', '40', '硕士']
4:['王五', '男', '30', '博士']
5:['李四', '男', '40', '博士']
6:['张三', '女', '20', '博士']
7:['张三', '男', '40', '硕士']
8:['李四', '男', '20', '硕士']
9:['王五', '男', '20', '学士']
10:['张三', '男', '30', '硕士']
11:['张三', '男', '40', '学士']
|