[Tutor] Difficulty with csv files - line breaks

Tim Goddard timgoddardsemail at gmail.com
Tue Nov 24 22:53:02 CET 2009


What I'm trying to do is store a bunch of information into a .csv
file.  Each row will contain a date, webpage, etc of a job
application.

My difficulty is that it seems something I am doing is not recording
the line breaks.  I've read that \r\n are default in the csv module
but so far I can not seem to use them successfully..So every time I
enter a new list of data it just gets appended to the end of the last
row in the .csv file.

It should read:
"date", "company", "title", "site", "site url", "ref_ID"
"date", "company", "title", "site", "site url", "ref_ID"

but instead it does this:
"date", "company", "title", "site", "site url", "ref_ID""date",
"company", "title", "site", "site url", "ref_ID" and so forth....

Here's the segment of code responsible for my troubles.

import csv

                date = raw_input("Enter the date applied: ")
                company = raw_input("Enter the company applied to: ")
                job_title = raw_input("Enter the job title: ")
                site = raw_input("Enter the website used: ")
                site_url = raw_input("Paste the URL here: ")
                ref_ID = raw_input("Enter the reference ID: ")
                entry_list = [date, company, job_title, site, site_url, ref_ID]
                print "Are you sure you want to add\n",
                for entry in entry_list:
                    print entry
                print "to the file?"
                answer = yes_and_no()
                if answer == "y":
                    append_file(entry_list,filename)

def append_file(list,filename):
    text_file = open(filename, "a")
    writer = csv.writer(text_file, quoting=csv.QUOTE_NONNUMERIC)
    writer.writerow(list)
    text_file.close()


More information about the Tutor mailing list