0

I'm using following code to write data into csv file:

filename = "DETAILS.csv"
f = open(filename, "a")
f.write(
    school_name + "," + affiliation_no + "," + state + "," + district + ","   
    + postal_address + "," + phone_no + "," + email_id + "," + web_site + "," 
    + year_of_foundation + "," + date_of_opening + "," + name_of_principal + ","
    + status_of_school + "," + type_of_affiliation + "," + affiliation_period_from + ","
    + affiliation_period_to + "," + name_of_trust_society_managing_committee + "\n")

f.close()

I'm getting the output in this format:

The columns and data are not aligned in csv file.I need one row per each entry. Can anyone help me fix it?

6
  • make sure u strip all the variables before writing to csv. Commented Mar 10, 2017 at 9:59
  • You have space symbols and \n in the fields. Remove it. Commented Mar 10, 2017 at 10:03
  • Can you paste the first few rows from the file DETAILS.csv so that we can see what's actually in your data? This will help understand your problem. Commented Mar 10, 2017 at 10:04
  • Schmuddin i have provided few rows of the csv file in the image like.Please, find the image there. Commented Mar 10, 2017 at 10:08
  • I guess it's because there's , or \n in your data? Commented Mar 10, 2017 at 10:09

2 Answers 2

1

As a general rule, when you work with CSV files, you shouldn't use the standard file operations, but use the writer and reader classes from the csv module. These classes take care of quotation marks and of the character used to separate the columns in your file (the comma , in your case). They also automatically handle numbers and strings correctly.

So, your code becomes this:

import csv

filename = "DETAILS.csv"
f = open(filename, "a")

# create a CSV writer object:
csvwriter = csv.writer(f)

# create a list of values that will be written to the file:
dat = [school_name, affiliation_no, state, district, postal_address,
       phone_no, email_id, web_site, year_of_foundation, date_of_opening,
       name_of_principal, status_of_school, type_of_affiliation,
       affiliation_period_from, affiliation_period_to, 
       name_of_trust_society_managing_committee]

# write the list to the file, using ',' automatically to separate the 
# columns:
csvwriter.writerow(dat)

f.close()

But this alone may not be enough to solve your issue. It looks as if the program that you use to open the CSV file encounters one or more characters that it can't interpret correctly. As pointed out by others, the offending characters may be the tabulator character \t or the linebreak character \n, but as you seem to refuse to paste some of the actual content of your file, there is no way of knowing that for sure.

However, you can use the following code to replace these characters by a space. The list comprehension handles both numbers and strings:

dat = [val.replace("\n", " ")
          .replace("\t", " ") if type(val) is str else val for val in dat]

You can add this before the call to csvwriter.writerow(dat) in my code example to make sure that the problematic characters are removed before the row is written to the CSV file.

Sign up to request clarification or add additional context in comments.

Comments

0

It seems that you string is not a valid csv format.

You should make sure these things:

  • Remove the useless comma,if there is comma in your data,you should try to use double quotes, for example:

    data,"{'response':1}"

  • Remove the \n,\t character,they will mess up your data.

Have a look at Comma separated values from wikipedia.

Hope this helps.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.