Board logo

标题: [问题求助] Python如何读取最后一个TRACK对应的TITLE [打印本页]

作者: netdzb    时间: 2020-8-21 09:41     标题: Python如何读取最后一个TRACK对应的TITLE

TRACK 12 AUDIO
    TITLE "On The Road(Demo2)"
    PERFORMER "孙燕姿"
    INDEX 00 42:42:07
    INDEX 01 42:44:52

文件如果顺序从上往下读,检索每一个TRACK,当读到结束的时候再向上检索
TITLE吗?

========

REM DATE 2000
REM DISCID 980A4C0C
REM COMMENT "ExactAudioCopy v1.0b4"
PERFORMER "孙燕姿"
TITLE "我要的幸福"
FILE "孙燕姿 - 我要的幸福.wav" WAVE
  TRACK 01 AUDIO
    TITLE "On The Road(Demo1)"
    PERFORMER "孙燕姿"
    INDEX 01 00:00:00
  TRACK 02 AUDIO
    TITLE "我要的幸福"
    PERFORMER "孙燕姿"
    INDEX 01 01:25:27
  TRACK 03 AUDIO
    TITLE "坏天气"
    PERFORMER "孙燕姿"
    INDEX 00 04:58:40
    INDEX 01 05:01:05
  TRACK 04 AUDIO
    TITLE "零缺点"
    PERFORMER "孙燕姿"
    INDEX 00 09:30:22
    INDEX 01 09:32:72
  TRACK 05 AUDIO
    TITLE "开始懂了"
    PERFORMER "孙燕姿"
    INDEX 00 13:11:57
    INDEX 01 13:14:27
  TRACK 06 AUDIO
    TITLE "中间地带"
    PERFORMER "孙燕姿"
    INDEX 00 17:43:72
    INDEX 01 17:46:50
  TRACK 07 AUDIO
    TITLE "相信"
    PERFORMER "孙燕姿"
    INDEX 00 22:15:67
    INDEX 01 22:17:45
  TRACK 08 AUDIO
    TITLE "累赘"
    PERFORMER "孙燕姿"
    INDEX 00 26:47:22
    INDEX 01 26:49:65
  TRACK 09 AUDIO
    TITLE "难得一见"
    PERFORMER "孙燕姿"
    INDEX 00 30:01:30
    INDEX 01 30:03:72
  TRACK 10 AUDIO
    TITLE "害怕"
    PERFORMER "孙燕姿"
    INDEX 00 34:15:45
    INDEX 01 34:18:15
  TRACK 11 AUDIO
    TITLE "星期一天气晴 我离开你"
    PERFORMER "孙燕姿"
    INDEX 00 38:20:67
    INDEX 01 38:23:02
  TRACK 12 AUDIO
    TITLE "On The Road(Demo2)"
    PERFORMER "孙燕姿"
    INDEX 00 42:42:07
    INDEX 01 42:44:52
作者: Gin_Q    时间: 2020-8-21 09:45

读到一次TRACK,更新一下TITLE数据对象!
作者: netdzb    时间: 2020-8-21 10:46

回复 2# Gin_Q

贴个代码看看。
作者: wujunkai    时间: 2020-8-22 15:23

回复 3# netdzb
  1. >>> import re
  2. >>> text='''REM DATE 2000
  3. REM DISCID 980A4C0C
  4. REM COMMENT "ExactAudioCopy v1.0b4"
  5. PERFORMER "孙燕姿"
  6. TITLE "我要的幸福"
  7. FILE "孙燕姿 - 我要的幸福.wav" WAVE
  8.   TRACK 01 AUDIO
  9.     TITLE "On The Road(Demo1)"
  10.     PERFORMER "孙燕姿"
  11.     INDEX 01 00:00:00
  12.   TRACK 02 AUDIO
  13.     TITLE "我要的幸福"
  14.     PERFORMER "孙燕姿"
  15.     INDEX 01 01:25:27
  16.   TRACK 03 AUDIO
  17.     TITLE "坏天气"
  18.     PERFORMER "孙燕姿"
  19.     INDEX 00 04:58:40
  20.     INDEX 01 05:01:05
  21.   TRACK 04 AUDIO
  22.     TITLE "零缺点"
  23.     PERFORMER "孙燕姿"
  24.     INDEX 00 09:30:22
  25.     INDEX 01 09:32:72
  26.   TRACK 05 AUDIO
  27.     TITLE "开始懂了"
  28.     PERFORMER "孙燕姿"
  29.     INDEX 00 13:11:57
  30.     INDEX 01 13:14:27
  31.   TRACK 06 AUDIO
  32.     TITLE "中间地带"
  33.     PERFORMER "孙燕姿"
  34.     INDEX 00 17:43:72
  35.     INDEX 01 17:46:50
  36.   TRACK 07 AUDIO
  37.     TITLE "相信"
  38.     PERFORMER "孙燕姿"
  39.     INDEX 00 22:15:67
  40.     INDEX 01 22:17:45
  41.   TRACK 08 AUDIO
  42.     TITLE "累赘"
  43.     PERFORMER "孙燕姿"
  44.     INDEX 00 26:47:22
  45.     INDEX 01 26:49:65
  46.   TRACK 09 AUDIO
  47.     TITLE "难得一见"
  48.     PERFORMER "孙燕姿"
  49.     INDEX 00 30:01:30
  50.     INDEX 01 30:03:72
  51.   TRACK 10 AUDIO
  52.     TITLE "害怕"
  53.     PERFORMER "孙燕姿"
  54.     INDEX 00 34:15:45
  55.     INDEX 01 34:18:15
  56.   TRACK 11 AUDIO
  57.     TITLE "星期一天气晴 我离开你"
  58.     PERFORMER "孙燕姿"
  59.     INDEX 00 38:20:67
  60.     INDEX 01 38:23:02
  61.   TRACK 12 AUDIO
  62.     TITLE "On The Road(Demo2)"
  63.     PERFORMER "孙燕姿"
  64.     INDEX 00 42:42:07
  65.     INDEX 01 42:44:52'''
  66. >>> re.findall(r'TRACK.+?[\n].+?TITLE.+?[\n]',text)
  67. ['TRACK 01 AUDIO\n    TITLE "On The Road(Demo1)"\n', 'TRACK 02 AUDIO\n    TITLE "我要的幸福"\n', 'TRACK 03 AUDIO\n    TITLE "坏天气"\n', 'TRACK 04 AUDIO\n    TITLE "零缺点"\n', 'TRACK 05 AUDIO\n    TITLE "开始懂了"\n', 'TRACK 06 AUDIO\n    TITLE "中间地带"\n', 'TRACK 07 AUDIO\n    TITLE "相信"\n', 'TRACK 08 AUDIO\n    TITLE "累赘"\n', 'TRACK 09 AUDIO\n    TITLE "难得一见"\n', 'TRACK 10 AUDIO\n    TITLE "害怕"\n', 'TRACK 11 AUDIO\n    TITLE "星期一天气晴 我离开你"\n', 'TRACK 12 AUDIO\n    TITLE "On The Road(Demo2)"\n']
  68. >>> last=re.findall(r'TRACK.+?[\n].+?TITLE.+?[\n]',text)[-1]
  69. >>> last
  70. 'TRACK 12 AUDIO\n    TITLE "On The Road(Demo2)"\n'
  71. >>> last_title=re.search(r'(?<=").+?(?=")',last).group()
  72. >>> last_title
  73. 'On The Road(Demo2)'
复制代码

作者: wujunkai    时间: 2020-8-22 15:24

善用正则
整理一下,大概是
  1. print(re.search(r'(?<=").+?(?=")',re.findall(r'TRACK.+?[\n].+?TITLE.+?[\n]',text)[-1]).group())
复制代码

作者: netdzb    时间: 2020-8-22 16:43

回复 5# wujunkai

谢谢,我学习学习。




欢迎光临 批处理之家 (http://www.bathome.net/) Powered by Discuz! 7.2