这是我的数据:
ID,application_id,award_notice_date,budget_start,budget_end,core_project_num,ed_inst_type 1,3000011,7/1/1985,6/30/1986,A03AH000859,公共卫生学校,2,3000012,7/1/1985,6/30/1986,A03AH000860,公共卫生学校,3,3000013,7/1/1985,6/30/1986,A03AH000861,公共卫生学校
我想要的是:
"ID“、"application_id”、"budget_start“、"budget_end”、"core_project_num“、"ed_inst_type”1 3000011、"7/1/1985“、”6/30/1986“、"A03AH000859”、“公共卫生学校”2 3000012、"7/1/1985“、”6/30/1986“、"A03AH000860”、“公共卫生学校”3 3000013、"7/1/1985“、"6/30/1986","A03AH000861",“公共卫生学校”
这是我的代码:
import csv
import sys
input_file = str(sys.argv[1])
output_file = str(sys.argv[2])
ifile = open(input_file)
reader = csv.reader(ifile)
ofile = open(output_file, 'w')
writer = csv.writer(ofile, delimiter=',', quoting=csv.QUOTE_NONNUMERIC)
for row in reader:
writer.writerow(row)问题:为所有数据添加双引号(包括数值数据和非数字数据)
"ID“、"application_id”、"budget_start“、"budget_end”、"core_project_num“、"ed_inst_type”"1“、"3000011”、"7/1/1985“、"6/30/1986”、"A03AH000859“、”公共卫生学校“2、"3000012”、"7/1/1985“、"6/30/1986”、"A03AH000860“、”公共卫生学校“"3","3000013“、”1985年7月1日“、”1986年6月30日“、"A03AH000861”、“公共卫生学校”
发布于 2016-01-22 23:36:45
可以将整数字段转换为整数值,如下所示:
for row in reader:
row = [int(x) if re.match(r'-?\d+$', x) else x for x in row]
writer.writerow(row)只需加上
import re在你的节目开始时。
https://stackoverflow.com/questions/34957004
复制相似问题