Python对文本文件的简单操作(一)

工作背景

  性能测试工程师,主要测试工具--loadrunner,主要是接口测试。

实现功能

  loadrunner对报文格式的转换存在问题,部分报文无法转换,故使用Python编写脚本自动将soap协议报文转换为loadrunner默认的格式。

转换步骤

soap协议原报文如下:

 

 1 <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ser="URL">
 2    <soapenv:Header/>
 3    <soapenv:Body>
 4       <ser:queryRiskPolicyCaseInfo>
 5          
 6          <arg0>
 7             
 8             <clientInfo>
 9                
10                <batchNo>?batchNo>
11                
12                <size>?size>
13             clientInfo>
14             
15             <riskPolicyCaseSubDto>
16                
17                <riskPolicyCaseRequestInfo>
18                   
19                   <companyCode>?companyCode>
20                   
21                   <currentPage>?currentPage>
22                   
23                   <districtCode>?districtCode>
24                   
25                   <districtLevel>?districtLevel>
26                   
27                   <endDate>?endDate>
28                   
29                   <pageSize>?pageSize>
30                   
31                   <riskCode>?riskCode>
32                   
33                   <riskRuleCode>?riskRuleCode>
34                   
35                   <ruleType>?ruleType>
36                   
37                   <startDate>?startDate>
38                riskPolicyCaseRequestInfo>
39             riskPolicyCaseSubDto>
40          arg0>
41       ser:queryRiskPolicyCaseInfo>
42    soapenv:Body>
43 soapenv:Envelope>

 

代码如下:

 1 import re
 2 """
 3 将soap格式的报文修改为loadrunner格式
 4 """
 5 file = r"C:\\Users\\zg\\Desktop\\报文转换\\报文.txt"
 6 file_out = r"C:\\Users\\zg\\Desktop\\报文转换\\转换后的报文.txt"
 7 #报文头部
 8 header = 'soap_request("StepName=google", \n"ExpectedResponse=AnySoap", \n'+'"URL=www.baidu.com'+'"'\
 9          +', \n"SOAPEnvelope= "\n""'
10 #报文尾部
11 last = '"Snapshot=t1.inf",\n"ResponseParam=result",\nLAST );'
12 with open(file_out,'w') as f_clear:
13     #写文件前清空文件
14     f_clear.truncate()
15     f_clear.write(header+'\n')
16 #确定line_num
17 line_num = 1
18 #确定文件的行数
19 numbers = len(open(file,'r').readlines())
20 #编辑原报文
21 with open(file) as f_in:
22     for line in f_in.readlines():
23         #判断是否为空行
24         if len(line.strip()) > 0:
25             # 找出每行第一个非空字符的位置(正则表达式)
26             num = re.search(r'\S', line).span()[0]
27            #判断是否为最后一行
28             if line_num < numbers:
29                 # 对每行进行拼接
30                 line = line[:num] + '"' + line[num:].replace('"', r'\"').rstrip() + '"'
31                 line_num += 1
32             else:
33                 line = line[:num] + '"' + line[num:].replace('"', r'\"').rstrip() + '"' + ','
34        #在最后一行行尾添加逗号‘,’
35         else:
36             line = '\r\t\n'
37             line_num += 1
38         #将修改后的内容写入新的文件
39         with open(file_out,'a') as f_out:
40             f_out.write(line + "\n")
41     #追加报文尾部
42     with open(file_out,'a') as f_out:
43         f_out.write(last)            

运行代码,转换后的报文如下:

 1 soap_request("StepName=google", 
 2 "ExpectedResponse=AnySoap", 
 3 "URL=www.baidu.com", 
 4 "SOAPEnvelope= "
 5 "xml version=\"1.0\" encoding=\"utf-8\"?>"
 6 "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ser=\"URL\">"
 7    "/>"
 8    "<soapenv:Body>"
 9       "<ser:queryRiskPolicyCaseInfo>"
10          ""
11          "<arg0>"
12             ""
13             "<clientInfo>"
14                ""
15                "<batchNo>?batchNo>"
16                ""
17                "<size>?size>"
18             "clientInfo>"
19             ""
20             "<riskPolicyCaseSubDto>"
21                ""
22                "<riskPolicyCaseRequestInfo>"
23                   ""
24                   "<companyCode>?companyCode>"
25                   ""
26                   "<currentPage>?currentPage>"
27                   ""
28                   "<districtCode>?districtCode>"
29                   ""
30                   "<districtLevel>?districtLevel>"
31                   ""
32                   "<endDate>?endDate>"
33                   ""
34                   "<pageSize>?pageSize>"
35                   ""
36                   "<riskCode>?riskCode>"
37                   ""
38                   "<riskRuleCode>?riskRuleCode>"
39                   ""
40                   "<ruleType>?ruleType>"
41                   ""
42                   "<startDate>?startDate>"
43                "riskPolicyCaseRequestInfo>"
44             "riskPolicyCaseSubDto>"
45          "arg0>"
46       "ser:queryRiskPolicyCaseInfo>"
47    "soapenv:Body>"
48 "soapenv:Envelope>",
49 "Snapshot=t1.inf",
50 "ResponseParam=result",
51 LAST );
View Code

  后期会使用Tkinter模块实现其图形界面化,代码中用到正则表达式模块re,以后会有专门的文章对其进行描述,本人为Python入门菜鸟,大神请绕过,不喜勿喷!

转载于:https://www.cnblogs.com/zhang-zhi/p/7646923.html

你可能感兴趣的:(Python对文本文件的简单操作(一))