如何使用python对齐CSV文件中的数据 [英] how to align data in CSV file using python

查看:1296
本文介绍了如何使用python对齐CSV文件中的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码将数据写入csv文件:

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:

csv文件中的列和数据未对齐.每个条目我需要一行.有人可以帮我解决这个问题吗?

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

推荐答案

作为一般规则,使用CSV文件时,不应使用标准文件操作,而应使用writerreader类来自 csv 模块.这些类处理引号和用于分隔文件中各列的字符(在您的情况下为逗号,).它们还会自动正确处理数字和字符串.

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.

因此,您的代码变为:

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()

但是仅此一项可能不足以解决您的问题.看来您用来打开CSV文件的程序遇到了一个或多个无法正确解释的字符.正如其他人指出的那样,令人讨厌的字符可能是制表符\t或换行符\n,但是由于您似乎拒绝粘贴文件的某些实际内容,因此无法得知当然.

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]

您可以在我的代码示例中对csvwriter.writerow(dat)的调用之前添加此代码,以确保在将行写入CSV文件之前已删除有问题的字符.

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.

这篇关于如何使用python对齐CSV文件中的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆