惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

The GitHub Blog
The GitHub Blog
博客园 - 三生石上(FineUI控件)
V
V2EX
博客园 - 司徒正美
小众软件
小众软件
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
T
Tailwind CSS Blog
Last Week in AI
Last Week in AI
雷峰网
雷峰网
月光博客
月光博客
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Apple Machine Learning Research
Apple Machine Learning Research
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
S
SegmentFault 最新的问题
美团技术团队
Hugging Face - Blog
Hugging Face - Blog
WordPress大学
WordPress大学
宝玉的分享
宝玉的分享
爱范儿
爱范儿
博客园 - 聂微东
量子位
J
Java Code Geeks
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Vercel News
Vercel News

夜行人

回家路上 第一期的直播演示项目 震动检测器 正能量 在线参观CodeLab Neverland 发布 CodeLab Adapter 3.3.1 DynamicTable 之 纸糊方向盘 CodeLab DynamicTable: 一个可实施的技术方案 CodeLab Insight 发布 Alpha 版 情人节 Home Assistant 周报 && IoT 周报 (02) Joplin: 关注隐私的 Evernote 开源替代软件 浏览器的未来与 Web 传感器 Home Assistant 周报 && IoT 周报 (01) 百宝箱(01) 论自由 介绍 WebThings Home Assistant 周报 && iot 周报 (00) 百宝箱(00) 毛姆读书心得 传世之作 周末徒步 CodeLab Adapter ❤️ Jupyter/Python 航班 躲雨 夏令营途中 [译]思想--作为一种技术 The future of coding 美国之行 三门问题的程序模拟
正则表达式之处理一组lrc听力文件后续
2015-06-12 · via 夜行人

此前写过这篇:正则表达式之处理一组lrc听力文件

室友近期在学Python,觉得上篇用的方法不够直观,理解起来有些费力,希望看到一个用正则找到文本之后进行替换的demo~ 作为中国好室友,我当然要给出demo啦

需求描述为:将时间格式改为以秒为单位。eg:[01:11.36]=>[71.36]

#待处理文本

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
:::text
[00:00.12]section 2.
[00:02.68]you will part of a radio programme about the opening of a new local sports shop.
[00:09.32]first you have some time to look at questions 11 to 16.
[00:39.64]now listen carefully and answer questions 11 to 16.
[00:48.24]now we go to Jane who is going to tell us about what's happening in town this weekend.
[00:52.24]right,thanks Andrew,
[00:53.92]and now on to what's new,
[00:56.48]and do we really need yet another sports shop in Bradcaster?
[01:01.24]well,most of you probably know Sports World-
[01:04.44]the branch of a Danish sports goods company that opened a few years ago-
[01:09.04]it's attracted a lot of custom,
[01:11.36]and so the company has now decided to open another branch in the area.
[01:16.60]it's going to be in the shopping centre to the west of Bradcaster,
[01:20.44]so that will be good news for all of you who've found the original shop in the north of the town hard to get to.
[01:27.12]i was invited to a special preview
[01:29.60]and i can promise you,this is the ultimate in sports retailing.

......

#解决方案

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#!/usr/bin/env python
# encoding: utf-8
import re
from decimal import Decimal as D

def min_sec_str2sec_str(match):
    min_sec_str = match.groupdict("thetime")
    #return min_sec_str
    '''minute:second change to second'''
    (m,s) = min_sec_str.split(":")
    sec = 60*D(m)+D(s)
    sec_str = str(sec)
    return sec_str

def format_file_time(filename):
    outputfile = open("output.lrc","w")
    #命名分组
    thetime = re.compile(r"(?P<thetime>\d{2}:.{5})")
    with open(filename,"r") as input_file:
      for line in input_file:
        format_line = thetime.sub(min_sec_str2sec_str,line)
        #print format_line
        outputfile.write(format_line)
    outputfile.close()

文章作者 种瓜

上次更新 2015-06-12