从文本文件中提取列表列表

时间:2017-12-13 01:58:29

标签: python-3.x file-io nested-lists

我有一个充满音乐歌词的文本文件。一个例子:

NAME: The Cave SINGER: Mumford and Sons

It's empty in the valley of your heart
The sun, it rises slowly as you walk
Away from all the fears
And all the faults you've left behind

The harvest left no food for you to eat
You cannibal, you meat-eater, you see
But I have seen the same
I know the shame in your defeat

But I will hold on hope
And I won't let you choke
On the noose around your neck

如何拉出它以便将其存储为列表列表?

Output: [[It's empty in the valley of your heart], [The sun, it rises slowly 
 as you walk] [Away from all the fears], [And all the faults you've left 
 behind] etc.]

有多首歌曲,每首歌曲都必须是他们自己的列表。

 with open("songs.txt", "r") as new:
       doc = new.readline()
       lyrics = []
       for line in doc:
            if "NAME:" not in line:
                 lyrics.append([line])

有一种简单的方法吗?

1 个答案:

答案 0 :(得分:0)

你非常接近,这是你可以使用的:

def read_file(filename):
    lyrics = []

    with open(filename) as file:
        next(file)
        for line in file.readlines():
            line = line.strip()
            if line:
                lyrics.append([line])

    return lyrics

print(read_file("songs.txt"))
# [["It's empty in the valley of your heart"], ['The sun, it rises slowly as you walk'], ['Away from all the fears'], ["And all the faults you've left behind"], ['The harvest left no food for you to eat'], ['You cannibal, you meat-eater, you see'], ['But I have seen the same'], ['I know the shame in your defeat'], ['But I will hold on hope'], ["And I won't let you choke"], ['On the noose around your neck']]

修改

如果您有多首歌曲,例如以下文件:

NAME: The Cave SINGER: Mumford and Sons

It's empty in the valley of your heart
The sun, it rises slowly as you walk
Away from all the fears
And all the faults you've left behind

The harvest left no food for you to eat
You cannibal, you meat-eater, you see
But I have seen the same
I know the shame in your defeat

But I will hold on hope
And I won't let you choke
On the noose around your neck

NAME: I Will Wait SINGER: Mumford and Sons

Well I came home
Like a stone
And I fell heavy into your arms
These days of dust
Which we've known
Will blow away with this new sun

But I'll kneel down
Wait for now
And I'll kneel down
Know my ground

And I will wait, I will wait for you
And I will wait, I will wait for you

您可以将这些歌曲存储在词典中:

import re

def read_file(filename):

    # dictionary of all the songs
    all_songs = {}

    with open(filename) as file:

        # convert file into one big string
        file_str = file.read()

        # get the substring locations of "Name"
        song_locs = [s.start() for s in re.finditer("NAME", file_str)]


        # get the songs into a list based on locations
        songs = []
        for start, end in zip(song_locs[:-1], song_locs[1:]):
            songs.append(file_str[start:end])

        # get last song
        songs.append(file_str[end:])


        # add each song to dictionary
        for song in songs:

            # get each line splitted by newline
            lines = [line for line in song.split("\n") if line]

            # adding song title/singer as key to dictionary, and lyrics as the values
            song_title, lyrics = lines[0], [[line] for line in lines[1:]]
            all_songs[song_title] = lyrics

        return all_songs

print(read_file("songs.txt"))

哪个输出:

{'NAME: The Cave SINGER: Mumford and Sons': [["It's empty in the valley of your heart"], ['The sun, it rises slowly as you walk'], ['Away from all the fears'], ["And all the faults you've left behind"], ['The harvest left no food for you to eat'], ['You cannibal, you meat-eater, you see'], ['But I have seen the same'], ['I know the shame in your defeat'], ['But I will hold on hope'], ["And I won't let you choke"], ['On the noose around your neck']], 'NAME: I Will Wait SINGER: Mumford and Sons': [['Well I came home'], ['Like a stone'], ['And I fell heavy into your arms'], ['These days of dust'], ["Which we've known"], ['Will blow away with this new sun'], ["But I'll kneel down"], ['Wait for now'], ["And I'll kneel down"], ['Know my ground'], ['And I will wait, I will wait for you'], ['And I will wait, I will wait for you']]}

你可以访问每首歌曲:

>>> d = read_file("songs.txt")
>>> print(d['NAME: The Cave SINGER: Mumford and Sons'])
[["It's empty in the valley of your heart"], ['The sun, it rises slowly as you walk'], ['Away from all the fears'], ["And all the faults you've left behind"], ['The harvest left no food for you to eat'], ['You cannibal, you meat-eater, you see'], ['But I have seen the same'], ['I know the shame in your defeat'], ['But I will hold on hope'], ["And I won't let you choke"], ['On the noose around your neck']]

>>>> print(d['NAME: I Will Wait SINGER: Mumford and Sons'])
[['Well I came home'], ['Like a stone'], ['And I fell heavy into your arms'], ['These days of dust'], ["Which we've known"], ['Will blow away with this new sun'], ["But I'll kneel down"], ['Wait for now'], ["And I'll kneel down"], ['Know my ground'], ['And I will wait, I will wait for you'], ['And I will wait, I will wait for you']]

注意:字典假定您将提供歌曲的NAME和SINGER。