Python simple text processing method,

Source: Internet
Author: User

Python simple text processing method,

This article describes how to process python simple text. Share it with you for your reference. The details are as follows:

Due to the influence of multiple threads, the time sequence printed by c ++ projects is inconsistent, which leads to poor statistics in excel. Therefore, I wrote a segment script in python to solve this problem. The following aspects are involved:

1. Reading txt text, utf8 Processing
2. Basic string operations
3. Basic dict operations
4. Basic list (array) Operations

#!/usr/bin/python#print "Hello World"str_seperator = "=================================================================================="timePointName = ["enter OpenNextImage at",#0     "enter OpenImage at",#1     "In OpenImage send On_ImageRefresh at",#2     "leave OpenImage at",#3     "leave OpenNextImage at",#4     "enter LoadImage at",#5     "decode began at",#6     "enter DrawClient at",#7     "leave DrawClient at",#8     "decode end at",#9     "in LoadImage send On_ImageRefresh at",#10     "leave loadImage at",#11     "second enter DrawClient at",#12     "second leave DrawClient at" #13     ]itemNumber= 0;avgTotal = 0; #13-0avgFirstDraw = 0; #8-2avgLoadImage = 0; #11-5avgSecondDraw = 0;#13-10fobj = open("F:\log.txt","r")imageTimeSta = {}dic = {}path = ""idx = 0for line in fobj:  idx = idx + 1  if idx == 1:    line = line[3:]  else:    pass  line = line.strip()  line = line.decode("utf-8").encode("gbk")  if line == str_seperator:    if path == "":      pass    else:      imageTimeSta[path] = dic    dic = {}    path = ""    continue  tabIndex = line.find('\t')  if tabIndex == -1:    path = line    print path    continue  tabLastIndex = line.rfind('\t')  name = line[0:tabIndex]  time = int(line[tabLastIndex + 1:])  if name in dic:    dic["second " + name] = time  else:    dic[name] = timefobj.close()itemNumber = len(imageTimeSta)keys = imageTimeSta.keys();for (k,dic) in imageTimeSta.iteritems():  avgTotal += dic[timePointName[13]] - dic[timePointName[0]];  avgFirstDraw += dic[timePointName[8]] - dic[timePointName[2]];  avgLoadImage += dic[timePointName[11]] - dic[timePointName[5]];  avgSecondDraw += dic[timePointName[13]] - dic[timePointName[10]];print 'avgTotal',avgTotal / float(itemNumber)print 'avgFirstDraw',avgFirstDraw / float(itemNumber)print 'avgLoadImage',avgLoadImage / float(itemNumber)print 'avgSecondDraw',avgSecondDraw / float(itemNumber)#print imageTimeSta

The log.txt file is as follows:

Please enter OpenNextImage at 5124D: \ pics \ test image \ decoding performance comparison diagram \ jpeg \ small image \ 2. JPGenter OpenImage at 5124In OpenImage send messages at 5124 enter LoadImage at 5124 leave OpenImage at 5124 leave OpenNextImage at 5124 decode began at 5124 enter DrawClient at 5140 leave DrawClient at 5155 decode end at 5265in LoadImage send messages at 5265 leave loadImage at 5265 enter DrawClient at 5280 leave DrawClient at 5327 ==================== ========================================================== ================== enter OpenNextImage at 6280D: \ pics \ test image \ decoding performance comparison diagram \ jpeg \ small image \ 3. JPGenter OpenImage at 6280In OpenImage send messages at 6280 enter LoadImage at 6280 leave OpenImage at 6296 leave OpenNextImage at 6296 decode began at 6296 enter DrawClient at 6296 leave DrawClient at 6312 decode end at 6437in LoadImage send messages at 6437 enter DrawClient at 6437 leave loadImage at 6452 leave DrawClient at 6499 ==================== ========================================================== ================== enter OpenNextImage at least 5d: \ pics \ test image \ decoding performance comparison diagram \ jpeg \ small image \ 4. JPGenter OpenImage at least 5in OpenImage send messages at 7265 leave OpenImage at 7265 leave OpenNextImage at 7265 enter LoadImage at 7265 decode began at 7265 enter DrawClient at 7265 leave DrawClient at 7296 decode end at 7421in LoadImage send messages at 7421 enter DrawClient at 7421 leave loadImage at 7437 leave DrawClient at 7483 ==================== ========================================================== ================== enter OpenNextImage at 8062D: \ pics \ test image \ decoding performance comparison diagram \ jpeg \ small image \ 5. JPGenter OpenImage at 8062In OpenImage send comment at 8062 leave OpenImage at 8062 leave OpenNextImage at 8062 enter LoadImage at 8062 decode began at 8062 enter DrawClient at 8062 leave DrawClient at 8077 decode end at 8202in LoadImage send Comment at 8202 enter DrawClient at 8202 leave DrawClient at 8265 leave loadImage at 8280 ==================== ========================================================== ================== enter OpenNextImage at 8811D: \ pics \ test image \ decoding performance comparison diagram \ jpeg \ small image \ 6. JPGenter OpenImage at 8811In OpenImage send messages at 8811 leave OpenImage at 8811 leave OpenNextImage at 8811 enter LoadImage at 8811 decode began at 8811 enter DrawClient at 8811 leave DrawClient at 8843 decode end at 8968in LoadImage send messages at 8968 leave loadImage at 8968 enter DrawClient at 8968 leave DrawClient at 9030 ==================== ========================================================== ================== enter OpenNextImage at 9515D: \ pics \ test image \ decoding performance comparison diagram \ jpeg \ small image \ 7. JPGenter OpenImage at 9515In OpenImage send comment at 9515 leave OpenImage at 9515 leave OpenNextImage at 9515 enter LoadImage at 9515 decode began at 9530 enter DrawClient at 9530 leave DrawClient at 9546 decode end at 9671in LoadImage send Comment at 9671 enter DrawClient at 9671 leave loadImage at 9671 leave DrawClient at 9733 ==================== ========================================================== ================== enter OpenNextImage at 10171D: \ pics \ test image \ decoding performance comparison diagram \ jpeg \ small image \ 8. JPGenter OpenImage at 10171In OpenImage send comment at 10171 leave OpenImage at 10171 leave OpenNextImage at 10171 enter LoadImage at 10171 decode began at 10186 enter DrawClient at 10186 leave DrawClient at 10202 decode end at 10311in LoadImage send Comment at 10311 leave loadImage at 10311 enter DrawClient at 10311 leave DrawClient at 10374 ==================== ========================================================== ================== enter OpenNextImage at least 11d: \ pics \ test image \ decoding performance comparison image \ jpeg \ small image \ 9. JPGenter OpenImage at least 11in OpenImage send messages at 10811 enter LoadImage at 10811 leave OpenImage at 10811 leave OpenNextImage at 10811 enter DrawClient at 10811 decode began at 10811 leave DrawClient at 10843 decode end at least 52in LoadImage send messages at 10952 leave loadImage at 10952 enter DrawClient at 10952 leave DrawClient at 11030 ==================== ========================================================== ================== enter OpenNextImage at 11452D: \ pics \ test image \ decoding performance comparison image \ jpeg \ small image \ 10. JPGenter OpenImage at 11452In OpenImage send comment at 11452 leave OpenImage at 11452 leave OpenNextImage at 11452 enter LoadImage at 11452 decode began at 11452 enter DrawClient at 11468 leave DrawClient at 11483 decode end at 93115in LoadImage send Comment at 11593 enter DrawClient at 11593 leave loadImage at 11608 leave DrawClient at 11655 ==================== ========================================================== ================== enter OpenNextImage at 12077 enter DrawClient at 12077 leave DrawClient at 12108 ========== ========================================================== ============================== enter OpenNextImage at 13124D: \ pics \ test image \ decoding performance comparison image \ jpeg \ small image \ 1.jpg enter OpenImage at 13124In OpenImage send On_ImageRefresh at 13124 leave OpenImage at 13124 leave OpenNextImage at 13124 enter LoadImage at 13124 decode began at 13124 enter DrawClient at 13139 leave DrawClient at 13155 decode end at 13358in LoadImage send On_ImageRefresh at 13358 leave loadImage at 13358 enter DrawClient at 13358 leave DrawClient at 13405 ====== ========================================================== ========================================

I hope this article will help you with Python programming.

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.