Reputation: 13
I'm currently trying to work with data about artists, their songs, and the lyrics. I have csv with the artist, the song name, and the lyrics in that order. I'm trying to split it so that I have each thing separate however the lyrics keep getting split whenever there is a new line. I've tried using this.
fp = open('songdata_test.csv', 'r')
for line in fp:
line_lst = line.split(',')
However that just returned the error previously described. Does anyone know how to split this csv so that the lyrics do not get split?
Edit: Example of what I'm trying to split.
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..."
Upvotes: 1
Views: 92
Reputation: 3457
Parsing a csv with lyrics has some non-trivial problems that are difficult to handle by yourself (I can see from your edition that you already figured this out). In particular, columns delimited by quotes and new lines or commas inside the data itself are difficult to parse and there are modules already designed for such tasks.
I suggest trying with python's csv.reader or, better, with pandas.
Using csv.reader
From the documentation:
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))
Using pandas
import pandas as pd
df = pd.read_csv('songdata_test.csv')
This will return a pandas DataFrame
object and handling it correctly will involve some learning, but if you use python and csvs with python I strongly suggest giving it a try.
Upvotes: 1