Python从文件中提取部分信息

时间:2016-12-08 08:59:52

标签: python parsing readfile

我想知道如何提取nfo文件的部分信息。我需要的是跟踪列表。该文件是一个nfo文件。

Encoder    : Flac 1.2.1

[Track List]

1.  The Chronic (Intro) (Feat. Snoop Doggy Dog)     1:57
2.  Fuck Wit Dre Day (And Everybody's Celebratini')  4:52
(Feat. Snoop Doggy Dog, RBX & Jewell)
3.  Let Me Ride (Feat. Snoop Doggy Dog, Ruben &     4:21
Jewell)
4.  The Day The Niggaz Took Over (Feat. Snoop       4:33
Doggy Dog, RBX & Dat Nigga Daz)
5.  Nuthin' But A "G" Thang (Feat. Snoop Doggy      3:58
Dog)
6.  Deeez Nuuuts (Feat. Snoop Doggy Dog)            5:06
7.  Lil' Ghetto Boy (Feat. Snoop Doggy Dog, Dat     5:29
Nigga Daz & Nate Dogg)

This is the 2009 Remastered..... 

我怎样才能从[Track List]到Dogg只获得部分而没有其他任何东西?

<td align='left' colspan='2'>Artist: Dr. Dre<br />
Album: The Chronic: Re-Lit &amp; From The Vault<br />
Label: WIDEawake/Death Row<br />
Genre: Hip-Hop<br />
Bitrate: 983 kbps avg<br />
Source: CD<br />
Playtime: 01:02:52 (444.MB)<br />
Rls date: 2011-10-06<br />
Store date: 2009-09-01<br />
Encoder: Flac 1.2.1 [Track List]<br />
1. The Chronic (Intro) (Feat. Snoop Doggy Dog) 1:57<br >
2. Fuck Wit Dre Day (And Everybody&#039;s          Celebratin&#039;) 4:52<br />
(Feat. Snoop Doggy Dog, RBX &amp; Jewell)<br />
3. Let Me Ride (Feat. Snoop Doggy Dog, Ruben &amp;    4:21<br />
Jewell)<br />
4. The Day The Niggaz Took Over (Feat. Snoop 4:33<br />
Doggy Dog, RBX &amp; Dat Nigga Daz)<br />
5. Nuthin&#039; But A &quot;G&quot; Thang (Feat. Snoop Doggy 3:58<br />
Dog)<br />
6. Deeez Nuuuts (Feat. Snoop Doggy Dog) 5:06<br />

This is the 2009 Remastered Edition of this classic    album</td>
                    </tr></table></div><br/><br/>

1 个答案:

答案 0 :(得分:1)

这可以让您全面了解可能有用的内容:

text = """Encoder    : Flac 1.2.1

[Track List]

1.  The Chronic (Intro) (Feat. Snoop Doggy Dog)     1:57
2.  Fuck Wit Dre Day (And Everybody's Celebratini')  4:52
(Feat. Snoop Doggy Dog, RBX & Jewell)
3.  Let Me Ride (Feat. Snoop Doggy Dog, Ruben &     4:21
Jewell)
4.  The Day The Niggaz Took Over (Feat. Snoop       4:33
Doggy Dog, RBX & Dat Nigga Daz)
5.  Nuthin' But A "G" Thang (Feat. Snoop Doggy      3:58
Dog)
6.  Deeez Nuuuts (Feat. Snoop Doggy Dog)            5:06
7.  Lil' Ghetto Boy (Feat. Snoop Doggy Dog, Dat     5:29
Nigga Daz & Nate Dogg)

This is the 2009 Remastered..... """

import re

tracks = []
parse = False
starttoken = "[Track List]"

for line in text.split('\n'):
    if starttoken in line:
        parse = True
        continue
    if not parse:
        continue

    m = re.match('(\d+)\.\s+(.*?)\s+(\d+:\d+)', line.strip())

    if m:
        tracks.append(list(m.groups()))
    elif line:
        tracks[-1][1] += ' ' + line
    elif tracks:
        break

for track in tracks:
    print("{:>02}) {} ({})".format(*track))

输出:

01) The Chronic (Intro) (Feat. Snoop Doggy Dog) (1:57)
02) Fuck Wit Dre Day (And Everybody's Celebratini') (Feat. Snoop Doggy Dog, RBX & Jewell) (4:52)
03) Let Me Ride (Feat. Snoop Doggy Dog, Ruben & Jewell) (4:21)
04) The Day The Niggaz Took Over (Feat. Snoop Doggy Dog, RBX & Dat Nigga Daz) (4:33)
05) Nuthin' But A "G" Thang (Feat. Snoop Doggy Dog) (3:58)
06) Deeez Nuuuts (Feat. Snoop Doggy Dog) (5:06)
07) Lil' Ghetto Boy (Feat. Snoop Doggy Dog, Dat Nigga Daz & Nate Dogg) (5:29)

这可能会使事情变得过于复杂,但我不确定您需要哪些信息。但是你明白了,可以根据自己的需要对其进行修改。