Reputation: 13
From a song lyrics, I have to fetch every word as elements without including any commas (,) in element. For example:
She loves you, yeah, yeah, yeah She loves you, yeah, yeah, yeah, yeah You think you lost your love Well, I saw her yesterday It's you she's thinking of And she told me what to say She says she loves you And you know that can't be bad
I do split the lyrics into list elements and then made them into lower case letters. Then I tried to find commas from the list and separated them. Now I want to remove commas (,) from the list elements.
Here is my code:
text_file = open("Beatles.txt", "r")
lines= text_file.read().split()
x.lower() for x in ["A","B","C"]]
re.findall(r"[\w]+|[.,!?;]", "Hello, I'm a string!")
My output is:
['she', 'loves', ',', 'you', ',', ',', ',', ',', 'yeah', ',', ',', 'yeah', ',', 'she', ',', 'loves', ',', 'you', ',', ',', 'yeah', ',', ',', 'yea']
My expected output is:
['she', 'loves', 'you', 'yeah', 'yeah', 'she', 'loves', 'you', 'yeah', 'yea']
Upvotes: 0
Views: 48
Reputation: 7887
You don't need regex to remove commas and lower case:
s = "She loves you, yeah, yeah, yeah She loves you, yeah, yeah, yeah, yeah You think you lost your love Well, I saw her yesterday It's you she's thinking of And she told me what to say She says she loves you And you know that can't be bad"
s = ''.join(c.lower() for c in s if c != ',')
print(s.split())
Output:
['she', 'loves', 'you', 'yeah', 'yeah', 'yeah', 'she', 'loves', 'you', 'yeah', 'yeah', 'yeah', 'yeah', 'you', 'think', 'you', 'lost', 'your', 'love', 'well', 'i', 'saw', 'her', 'yesterday', "it's", 'you', "she's", 'thinking', 'of', 'and', 'she', 'told', 'me', 'what', 'to', 'say', 'she', 'says', 'she', 'loves', 'you', 'and', 'you', 'know', 'that', "can't", 'be', 'bad']
Upvotes: 1