简体   繁体   中英

how to align data in CSV file using python

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?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM