Delete part of a text file using Python

Source: Internet
Author: User

In order to learn English, I converted the movie downloaded from the Internet into a pure MP3 file and put it in itouch, so that I can directly practice my listening. In addition, I also put the downloaded subtitles in, you can check it when you are not familiar with it. However, there is a problem that the subtitle formats uploaded on the Internet are as follows:

 7 <br/> 00:01:44, 789 --> 00:01:51, 019 <br/> title: When happiness comes to knock on the door/pursue happiness </P> <p> 8 <br/> 00:02:49, 469 --> 00:02:52, 839 <br/> coming soon <br/> shocould be here soon. </P> <p> 9 <br/> 00:03:38, 819 --> 00:03:41, 189 <br/>-what should I do for this column? <Br/>-I think I shoshould make a list.-What do mean? </P> <p> 10 <br/> 00:03:41, 189 --> 00:03:43, 789 <br/>-Do you want a birthday present? -Yes <br/>-for your birthday gifts? -Yeah.

As shown above, there are two problems: 1. Each sentence is preceded by a number; 2. It has a timestamp;

 

I only want the Chinese and English text in it, and the numbers and timestamps are not required. Since I was learning Python recently, I first thought of a Python script to delete the timestamp and number. The implementation is very simple. The basic idea is to use regular expression matching to delete empty rows, pure numeric rows, and rows with "-->", and then return to the text file first.

 

The code after completion is as follows:

#-*-Coding: UTF-8-*-</P> <p> # code = cp936 <br/> Import string, re </P> <p> title_txt = open ('I: // pursuit_for_happiness(chain.txt', 'r + ') <br/> try: <br/> full_txt = title_txt.readlines () <br/> regex1 = "/A/D * [/n]/z" <br/> regex2 = "/d -->/D" <br/> # fulprint full_txt <br/> new_txt = [] <br/> for line in full_txt: <br/> # print line <br/> If re. match (regex1, line) or Re. search (regex2, line): <br/> # print "match", line <br/> continue <br/> else: <br/> new_txt.append (line) </P> <p> title_txt.seek (0) <br/> title_txt.truncate (0) <br/> # For line in full_txt: <br/> # title_txt.writelines (line) <br/> title_txt.writelines (new_txt) </P> <p> finally: <br/> title_txt.close () </P> <p> Print "over"

 

A problem occurs during the process:

1. There is no way to clear a file in Python. After reading it, we found that the truncate () method can be used. It can be done when the input parameter is 0.

2. directly use for to traverse the elements in the list at the beginning, and delete the elements after matching. It is found that the deletion can be completed only after multiple scripts are executed, and some elements cannot be deleted at all times, finally, I finally figured it out: when using for I in list:, if it is deleted as soon as it matches, the elements in the list will change at this time and it will not be surprising if it enters the next round of the loop. It took nearly an hour to solve this problem. Therefore, an empty linked list new_txt is redefined to store the subtitles I want. After the loop is completed, new_txt is written back to the file. Success!

 

Cool, copy it to my itouch and start learning English!

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.