I have a csv file with formulas in columns 4, 5 and 6,
I also want to import data from the python program into columns 1, 2 & 3. however every time i do this using the write or append functions like so, the file just overwrites the formula's in 4,5 & 6.
Is there a way to stop this from happening?
from cv2 import cv2 as cv2
import numpy as np
import time
import math
import csv
from csv import DictWriter
#Reading the file first will check to make sure the file exists in the corresponding name format.
with open('wagonFrontData.csv', 'r') as userFile:
userFileReader = csv.reader(userFile)
# Note: when writing to a csv file the already existing file must be closed before working with the program
with open('wagonFrontData.csv', 'r', newline='') as f:
fieldnames=['X_Old', 'Y_Old','X_New', 'Y_New']
theWriter=csv.DictWriter(f, fieldnames=fieldnames)
# theWriter.writeheader()
#Function for appending data to the csv file.
def append_dict_as_row(file_name, dict_of_elem, field_names):
# Open file in append mode
with open(file_name, 'a+', newline='') as write_obj:
# Create a writer object from csv module
dict_writer = DictWriter(write_obj, fieldnames=field_names)
# Add dictionary as wor in the csv
dict_writer.writerow(dict_of_elem)
# specify the csv file to write to.
# parameter values https://www.w3schools.com/python/ref_func_open.asp
# https://docs.python.org/3/library/csv.html
while(1):
XOld=[1,2,3,4]
YOld=[2,3,4,5]
XNew=[3,6,7,8]
YNew=[4,4,6,7]
for i in range(0, len(XOld)):
row=({'X_Old':(XOld[i]), 'X_New':(XNew[i]), 'Y_Old':(YOld[i]), 'Y_New':(YNew[i])})
# print(row)
append_dict_as_row('wagonFrontData.csv',row,fieldnames)
userFileReader = csv.reader(userFile)doesn't read the file BTW.