# Get Billboard top ten from a text file into a .csv file

# A function that takes a line of the text file and
# cuts it into variables containing the different data
# items. 
def getFields(string):
    # The split operator, cuts the string at "
    fields = string.split('"')
    # fields now contains a list of strings.
    number = fields[0]
    artist = fields[1]
    song = fields[3]
    weeks = fields[4]
    # cut the space off the end of the week string
    number = number[:-1]
    # cut the space off the front, and the newline off
    # the end of the weeks string.
    weeks = weeks[1:-1]
    # Remove all references to T-Pain
    artist = artist.replace("Featuring T-Pain","")
    # Output file is global variable
    # This line writes an output string out to the
    # .csv fine
    outfile.write(number+","+artist+","+song+"," \
                  +weeks+"\n")

# The main program

# Open both input and output files
infile = open("billboard.txt","r")
outfile = open("billboard.csv","w")

# Skip first two lines
for i in range(2):
    inStr = infile.readline()

# Start reading at third line
inStr = infile.readline()
# While there are still lines in the file
while inStr != "": 
    getFields(inStr)
    # skip a line
    inStr = infile.readline()
    # Get next line with data in it
    inStr = infile.readline()

infile.close()
outfile.close()
