Python parsing File example

Source: Internet
Author: User
This article mainly introduces python parsing text file examples. For more information, see python. recently, the main task is to test component compatibility. The original framework has many functions that are not complete yet and need to be supplemented! For example, you need to write the execution result of the AutoIt script to Excel. The final solution is to use the local log to parse the result!

Added the following class to complete the above functions:

The code is as follows:


Class AutoItResultParser ():
Def ParseResult (self, vm_result, log_file ):
For case_result in vm_result.cases_results:
Self. _ ModifyAutoItResult (case_result, log_file)

Def _ ModifyAutoItResult (self, result, log_file ):
Items = []
Myfile = open (log_file, 'RB ')
Line = myfile. readline ()
Count = 0
While (''! = Line ):
Items. append (line. split (':') [0])
Count + = 1
If (count % 2 = 0 ):
Items. append (line. split (':') [1])
Line = myfile. readline ()

Myfile. close ()
Fail_scripts = []
Length = len (items)
Arr = list (range (2, length, 3 ))
For I in arr:
Test = items [I]. lower ()
If test. rfind ('success') =-1:
Fail_scripts.append (items [I-2], items [I-1])

For script in fail_scripts:
If script [0] = result. case_name:
If script [1] = 'installation ':
Result. install_script_success = False
Elif script [1] = 'launch ':
Result. launch_script_success = False
Elif script [1] = 'function ':
Result. function_script_success = False
Else:
Result. uninstall_script_success = False

The content of the log_file file is similar to the following:

The code is as follows:


VisualStudio2010_StandaloneProfiler:
Installation: Success
VisualStudio2010_StandaloneProfiler:
Launch: Success
VisualStudio2010_StandaloneProfiler:
Function: Fail
TaobaoBrowser_2.0.0:
CitrixOfflinePlugin_6.5:
Installation: Success
CitrixOfflinePlugin_6.5:
Function: Success
TrusteerRapport:
TNTShippingTools:
Installation: Success
TNTShippingTools:
Launch: Success
WGET_1.11.4:
Installation: Success
VisualStudio2010_StandaloneProfiler:
Uninstallation: Success
TNTShippingTools:
Uninstallation: Fail

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.