我想在csv文件的每一行中写入数据,而不是覆盖但是使用标题列。使用这段代码,我可以在文件的每一行写入数据,但也可以写入标题。我希望标题也位于列的顶部,然后在每个raw中只是为了写入数据。这个代码Im使用
import csv
c =csv.writer(open("C:/Config/Output/box.csv","ab"), lineterminator='\n')
c.writerow(['ColA','ColB'])
c.writerow([dfl_origin[0],dfl_origin[1]])
dfl_origin [0]和dfl_origin [1]是可变的。
好的,这是更多的代码
def calculate_doorframe_parameters(subfeature_dict):
global dfl_hor_thr , dfl_ver_thr
global dfl_origin
dfl_points_dict = {}
for subfeature in subfeature_dict:
if ("doorframelines" in subfeature):
dfl_points_dict[subfeature] = subfeature_dict[subfeature]
if len(dfl_points_dict) == 2: #Invalid Case for algorithm (Can be removed after confirmation)
dfl_hor_thr = abs(dfl_points_dict['doorframelines1'][0] - dfl_points_dict['doorframelines2'][0])
if len(dfl_points_dict) == 6:
dfl_hor_thr = abs(dfl_points_dict['doorframelines2'][0] - dfl_points_dict['doorframelines5'][0])
dfl_ver_thr = abs(dfl_points_dict['doorframelines6'][1] - dfl_points_dict['doorframelines4'][1])
#Find Origin of DoorFrame
dfl_origin_X = min([dfl_points_dict['doorframelines2'][0], dfl_points_dict['doorframelines5'][0],dfl_points_dict['doorframelines1'][0] , dfl_points_dict['doorframelines3'][0]])
dfl_origin_Y = min([dfl_points_dict['doorframelines2'][1], dfl_points_dict['doorframelines5'][1],dfl_points_dict['doorframelines1'][1] , dfl_points_dict['doorframelines3'][1]])
dfl_origin= [dfl_origin_X,dfl_origin_Y]
import csv
c =csv.writer(open("C:/Config/Output/box2.csv","ab"), lineterminator='\n')
c.writerow(['ColA ColB'])
c.writerow([dfl_origin[0],dfl_origin[1]])
这是结果
ColA, ColB
1033 87.0
ColA ColB
987 65.0
预期结果应为
ColA, ColB
1033 87.0
987 65.0
任何帮助?
答案 0 :(得分:0)
你可以使用这样的东西,
with open('names.csv', 'a') as csvfile:
fieldnames = ['first_name', 'last_name']
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
writer.writerow({'first_name': 'Baked', 'last_name': 'Beans'})
'一个'代表'追加'解决了覆盖问题,