以怪异的方式分割csv

时间:2018-11-19 03:09:23

标签: python csv

我目前正在尝试处理有关艺术家,他们的歌曲和歌词的数据。我按顺序将csv与艺术家,歌曲名和歌词一起使用。我正在尝试将其拆分,以使每件事都分开,但是每当有新行时,歌词就会不断拆分。我尝试过使用它。

fp = open('songdata_test.csv', 'r')

for line in fp:
    line_lst = line.split(',')

但是,该错误只返回了先前描述的错误。有谁知道如何拆分此csv,以使歌词不会拆分?

编辑:我要拆分的示例。

Adele,All I Ask,"[Verse 1]  
I will leave my heart at the door  
I won't say a word  
They've all been said before, you know..."

Bob Dylan,4Th Time Around,"When she said, ""Don't waste your words, they're 
just lies,""  
I cried she was deaf.  
And she worked on my face until breaking my eyes,  
Then said, ""What else you got left?""  
It was then that I got up to leave..."

1 个答案:

答案 0 :(得分:1)

用歌词解析csv有一些非平凡的问题,这些问题很难自己解决(我从您的版本中可以看出,您已经知道了)。特别是,数据本身内部用引号和新行或逗号分隔的列很难解析,并且已经为此类任务设计了模块。

我建议尝试使用python的csv.reader,或者最好使用pandas

使用year

从文档中:

csv.reader

使用import csv with open('songdata_test.csv') as csvfile: reader= csv.reader(csvfile, delimiter=',', quotechar='"') # These are the defaults, I'm just showing the explicitly. This is equivalent to csv.reader(csvfile) for row in reader: print(', '.join(row))

pandas

这将返回pandas import pandas as pd df = pd.read_csv('songdata_test.csv') 对象,正确处理它需要进行一些学习,但是如果您将python和csvs与python一起使用,我强烈建议您尝试一下。