二进制Classifi的Python格式csv数据

2024-04-29 16:04:16 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一些csv数据的格式如下:

headers = [artist_list, song_list, lyrics_track, lyrics_artist, lyrics]`, 

还有这个片段:

with open('lyrics.tsv', "rU") as f:
    reader = csv.reader(f, delimiter="\t")
    for i, line in enumerate(reader):
        print 'line[{}] = {}'.format(i, line)

印刷品:

(...)

line[808] = ['Pearl Jam', 'Wishlist', 'Wishlist', 'Pearl Jam', "I wish I was a neutron bomb\nfor once I could go off\nI wish I was a sacrifice\nbut somehow still lived on\nI wish I was a sentimental\nornament you hung on\nthe Christmas tree, I wish I was\nthe star that went on top\nI wish I was the evidence\nI wish I was the grounds\nfor fifty million hands upraised and opened toward the sky\nI wish I was a sailor with\nsomeone who waited for me\nI wish I was as fortunate\nas fortunate as me\nI wish I was a messenger\nand all the news was good\nI wish I was the full moon shining\noff a Camaro's hood\nI wish I was an alien\nat home behind the sun\nI wish I was the souvenir\nyou kept your house key on\nI wish I was the pedal break\nthat you depended on\nI wish I was the verb to trust\nand never let you down\nI wish I was a radio song\nthe one that you turned up\nI wish ..."]

现在我想使用数据进行分类,只保留所有行的lyrics,并为二进制值添加一列(始终相同,0),因此数据转换为:

 lyrics                                                   type

 (...)                                                   (...)

 I wish I was a neutron bomb\nfor once I could go off..    0

如何从上面的代码开始?你知道吗


Tags: csvthe数据youartistonasline