I have a CSV file with the following data:
Date,Profit/Losses
Jan-10,867884
Feb-10,984655
Mar-10,322013
Apr-10,-69417
May-10,310503
Jun-10,522857
Jul-10,1033096
Aug-10,604885
Sep-10,-216386
Oct-10,477532
Nov-10,893810
Dec-10,-80353
I have imported the file in python like so:
with open(csvpath, 'r', errors='ignore') as fileHandle:
lines = fileHandle.read()
I need to loop through these lines such that I extract just the months i.e. "Jan", "Feb", etc. and put it in a different list. I also have to somehow skip the first line i.e. Date, Profit/Losses which is the header.
Here's the code I wrote I so far:
months = []
for line in lines:
months.append(line.split("-")
When I try to print the months list though, it splits every single character in the file!!
Where am I going wrong here??
csvmodule is your friend. Alternativelypandaswould be a huge help.read, you do not have lines anymore. Yourlinesis one string andfor line in linesgoes over individual letters. See a proposed solution below.