要求:用 python 或伪代码写一个程序,输入 a.txt,输出 b.txt。
-
a.txt:
------左声道------ name:S,status:20,index:1,result:喂你好啊你这个商城就算是我这是通过那个朋友推荐的,time:19640,begin time:6520 name:S,status:20,index:2,result:嗯我想要那个什么就是看一下那个我们的茶品能不能进,time:30830,begin time:20860 name:S,status:20,index:3,result:对我我我这是的哈恩额偶顶你下哦是哦溶解第的的多大,time:37680,begin time:35520 name:S,status:20,index:4,result:那这个我关注了现在也是前几天那个朋友推荐给我的的,time:43530,begin time:38260 name:S,status:20,index:5,result:嗯嗯嗯嗯嗯讷讷讷,讷讷讷讷嫩,嗯嗯嗯嗯嗯嗯嗯恩恩,time:45630,begin time:44440 name:S,status:20,index:6,result:我们是呃有这个就是干活吧八八八,八八八啊俺多对多,time:68640,begin time:45750 name:S,status:20,index:7,result:哎对我们是公司是生产厂家假假假,假假假假爱家家居,time:78160,begin time:73930 name:S,status:20,index:8,result:填写入驻申请是吧,误解的基金佛啊娇女啦分解哦儿儿,time:90120,begin time:87480 name:S,status:20,index:9,result:那个那你不是充点钱芊芊奥结果金融界的可怜人感觉人,time:94840,begin time:91440 name:S,status:20,index:10,result:呃进入商城饿饿饿那可就偶分拿了冠军了暗绿色v六十,time:98920,begin time:97270 ------右声道------ name:S,status:20,index:1,result:你好你好你好你好你好你好你好你好你好你好你好你好,time:20850,begin time:19650 name:S,status:20,index:2,result:哦是这样哦是这样哦是这样哦是这样哦是这样哦是这样,time:32430,begin time:30840 name:S,status:20,index:3,result:是的呢是的呢是的呢是的呢是的呢是的呢是的呢是的呢,time:33430,begin time:32440 name:S,status:20,index:4,result:哈哈,没错,就是这样哈哈,没错,就是这样哈哈,没,time:35430,begin time:33440 name:S,status:20,index:5,result:好的,没问题好的,没问题好的,没问题好的,没问题,time:73900,begin time:68650 name:S,status:20,index:6,result:行,就这样行,就这样行,就这样行,就这样行,就这,time:79160,begin time:78180 name:S,status:20,index:7,result:您说您说您说您说您说您说您说您说您说您说您说您说,time:80480,begin time:79180 name:S,status:20,index:8,result:嗯嗯,可以嗯嗯,可以嗯嗯,可以嗯嗯,可以嗯嗯,可,time:85480,begin time:81480 name:S,status:20,index:9,result:您的地址是哪里?您的地址是哪里您的地址是哪里您的?,time:87460,begin time:85500 name:S,status:20,index:10,result:没有问题,就这样没有问题,就这样没有问题,就这样,time:97200,begin time:94940
-
b.txt:
[乙] 喂你好啊你这个商城就算是我这是通过那个朋友推荐的 [甲] 你好你好你好你好你好你好你好你好你好你好你好你好 [乙] 嗯我想要那个什么就是看一下那个我们的茶品能不能进 [甲] 哦是这样哦是这样哦是这样哦是这样哦是这样哦是这样 [甲] 是的呢是的呢是的呢是的呢是的呢是的呢是的呢是的呢 [甲] 哈哈,没错,就是这样哈哈,没错,就是这样哈哈,没 [乙] 对我我我这是的哈恩额偶顶你下哦是哦溶解第的的多大 [乙] 那这个我关注了现在也是前几天那个朋友推荐给我的的 [乙] 嗯嗯嗯嗯嗯讷讷讷,讷讷讷讷嫩,嗯嗯嗯嗯嗯嗯嗯恩恩 [乙] 我们是呃有这个就是干活吧八八八,八八八啊俺多对多 [甲] 好的,没问题好的,没问题好的,没问题好的,没问题 [乙] 哎对我们是公司是生产厂家假假假,假假假假爱家家居 [甲] 行,就这样行,就这样行,就这样行,就这样行,就这 [甲] 您说您说您说您说您说您说您说您说您说您说您说您说 [甲] 嗯嗯,可以嗯嗯,可以嗯嗯,可以嗯嗯,可以嗯嗯,可 [甲] 您的地址是哪里?您的地址是哪里您的地址是哪里您的? [乙] 填写入驻申请是吧,误解的基金佛啊娇女啦分解哦儿儿 [乙] 那个那你不是充点钱芊芊奥结果金融界的可怜人感觉人 [甲] 没有问题,就这样没有问题,就这样没有问题,就这样 [乙] 呃进入商城饿饿饿那可就偶分拿了冠军了暗绿色v六十
-
分析:
甲:客户 右声道 乙:客服 左声道 按 time / begin time / index 排序 摘取 result 对应信息
-
格式:
def func(a_file): with open(a_file,'r')as f: pass return b_file
-
步骤:
1,先将左声道内所有消息加上[乙],将右声道内所有消息加上[甲] 2,然后按时间排序 3,最后取出result对应的值
-
代码:正则,sorted,文件操作
import re def func(a_file): with open(a_file, 'r', encoding='utf-8')as f: """【1,循环读取每一条,每一条数据都加上[乙],直到右声道出现变成加[甲]】""" roles = "[乙]" l = [] for content in f: # for循环读取文件,一行一行读 content = content.strip() if "右声道" in content: roles = "[甲]" if content == "------左声道------" or content == "------右声道------": continue new_content = roles + ' ' + content + '\n' """【2,正则匹配摘取有用信息,组成字典放进列表】""" roles = re.findall('\[\w\]', new_content)[0] result = re.findall('result:(.+),time', new_content)[0] time = re.findall('time:(\d+),', new_content)[0] l.append({'roles': roles, "result": result, "time": int(time)}) """【3,按时间排序】""" sorted_l = sorted(l, key=lambda x: x['time']) """【4,将信息写入文件】""" new_file = 'b.txt' with open(new_file, 'a', encoding='utf-8')as f1: for line in sorted_l: f1.write(line['roles'] + ' ' + line['result'] + "\n") return new_file func('a.txt')
总感觉效率太低,有没有什么好的方法?
网友评论