我正在使用PyLyrics为一个项目制作歌词数据集。
我已经导入了数据集,安装了PyLyrics,现在试图让Python为数据集中的所有行生成歌词,但是它没有任何输入。
import pandas as pd
data = pd.read_csv(r'C:\Users\kk\Desktop\dba lyrics analysis\all_periods_joined_for_lyrics_1.csv', header=1)
df=pd.DataFrame(data, columns=['Position', 'Track Name','Artist','Streams','date','period'])
for index, row in df.iterrows():
try:
from PyLyrics import *
df.loc[index, 'Lyrics'] = PyLyrics.getLyrics(row['Artist'], row['Track Name'])
except:
pass
当我到达print(df)
时,输出是充满我的标题和'NaN'的表,而不是任何输出。此外,不会生成歌词列。
我该怎么办?
我是编码的新手,但是该论文即将到期,因此我们将不胜感激!
谢谢!
答案 0 :(得分:0)
我可以想象将class TestViewSet(
GenericViewSet,
mixins.ListModelMixin,
mixins.DestroyModelMixin,
):
queryset = models.Test.objects.select_related('user').all()
def get_queryset(self):
base_qs = super(TestViewSet, self).get_queryset()
if self.action == 'get_dropdown_options':
base_qs = base_qs.filter(
user_id=self.request.get_session()['user_id']
)[:100000]
return base_qs
的导入放到PyLyrics
块中掩盖了try-except
模块未安装在您的计算机上因而无法导入的事实:
PyLyrics
相反,将try:
from PyLyrics import *
except:
# If the import failed we can fix it here - but we just ignore it (pass)
pass
放在文件的开头,没有from PyLyrics import *
块,这样,如果未安装软件包,脚本将立即失败。在这里查看如何安装软件包:https://pypi.org/project/PyLyrics/。
以下代码对我来说很好。
try-except
songs.csv
from PyLyrics import PyLyrics
from pprint import pprint
from pandas import read_csv
songs = read_csv('songs.csv', header=None, names=['Artist','Title'])
for index, row in songs.iterrows():
print('Getting lyrics for', row.Artist, row.Title)
try:
lyrics = PyLyrics.getLyrics(row.Artist, row.Title)
except:
lyrics = None
print('Failed to get lyrics for', row.Artist, row.Title)
songs.loc[index, 'Lyrics'] = lyrics
pprint(songs)
"""
Getting lyrics for ErrorUndefined DoesNotExist
Failed to get lyrics for ErrorUndefined DoesNotExist
Getting lyrics for Taylor Swift Blank Space
Getting lyrics for Bon Jovi It's My Life
Artist Title Lyrics
0 ErrorUndefined DoesNotExist NaN
1 Taylor Swift Blank Space Nice to meet you, where you been?\nI could sho...
2 Bon Jovi It's My Life This ain't a song for the broken-hearted\nNo s...
"""