1

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)     
2
  • userFileReader = csv.reader(userFile) doesn't read the file BTW. Commented Apr 5, 2020 at 21:40
  • 1
    You can append rows but you can't append columns. You have to read the data, merge columns, then write back the file. Commented Apr 5, 2020 at 21:41

1 Answer 1

1

Welcome to Stack Overflow! The problem you described doesn't match the code you provided. I will provide here a solution to the problem you described:

  • You want to keep de values from columns 4, 5, 6
  • You want to insert new values in column 1, 2, 3

It is really not recommended to read and write to a file at the same time (unless you really know what you are doing). So here in my solution, I will write to a temporary file and when everything is finished, I will rename that temporary file to the name of the original file to overwrite it. I'm also presuming that the values you want to insert in the 3 first columns are found in the variable values_generated_to_insert_in_output

import csv
import os

values_generated_to_insert_in_output = [
    ['missile', 'claim', 'winter'],
    ['screw', 'mill', 'mean'],
    ['privilege', 'deadly', 'tap'],
    ['conference', 'intermediate', 'tune'],
]

with open('wagonFrontData.csv', newline='') as input_file:
    csv_reader = csv.reader(input_file)
    with open('output_file.csv', 'w', newline='') as output_file:
        csv_writer = csv.writer(output_file)
        for (old_colums, new_colums) in zip(csv_reader, 
                                            values_generated_to_insert_in_output):
            row_to_output = list(new_colums).extend(old_colums[3:6])
            csv_writer.writerow(row_to_output)

os.replace('output_file.csv', 'wagonFrontData.csv')

Is that what you wanted to do?

Sign up to request clarification or add additional context in comments.

1 Comment

yes this looks like it does what i need to do. I ended up just setting up the import in a query on the sheet as unless this is run at the end of the video in the python code for which data is being pulled. Otherwise the reading and writing repeatedly in the loop slows the process down. Obviously it is best to run the full read and import at the end of the code once the video run has finished playing. Thankyou for your time and solution!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.