代码生成 Python提取歌词中的全部英文歌词,存入列表English中,每行歌词为一个列表元素
时间: 2023-06-23 13:08:05 浏览: 273
以下是一个示例代码,用于提取歌词中的全部英文歌词,并将其存储在一个名为`English`的列表中,其中每行歌词为一个列表元素:
```python
import re
lyrics = """
Oh, life is bigger
It's bigger than you
And you are not me
The lengths that I will go to
The distance in your eyes
Oh no, I've said too much
I set it up
That's me in the corner
That's me in the spotlight
Losing my religion
Trying to keep up with you
And I don't know if I can do it
Oh no, I've said too much
I haven't said enough
"""
English = re.findall(r'\b[A-Za-z]+\b', lyrics)
print(English)
```
输出:
```python
['Oh', 'life', 'is', 'bigger', 'It', 's', 'bigger', 'than', 'you', 'And', 'you', 'are', 'not', 'me', 'The', 'lengths', 'that', 'I', 'will', 'go', 'to', 'The', 'distance', 'in', 'your', 'eyes', 'Oh', 'no', 'I', 've', 'said', 'too', 'much', 'I', 'set', 'it', 'up', 'That', 's', 'me', 'in', 'the', 'corner', 'That', 's', 'me', 'in', 'the', 'spotlight', 'Losing', 'my', 'religion', 'Trying', 'to', 'keep', 'up', 'with', 'you', 'And', 'I', 'don', 't', 'know', 'if', 'I', 'can', 'do', 'it', 'Oh', 'no', 'I', 've', 'said', 'too', 'much', 'I', 'haven', 't', 'said', 'enough']
```
这个示例代码使用Python的正则表达式模块`re`来匹配所有的英文单词。具体来说,它使用`\b[A-Za-z]+\b`的正则表达式来匹配所有由英文字母组成的单词,并将它们存储在名为`English`的列表中。
阅读全文