0

I have a python program that converts csv files to '^' delimiter files. The output files also has new headers.

cc = input("Enter Code \t")
cn = input("Enter Item Number \t")
input_file = input("Enter input file \t")
act = input("Enter Action (Update- N/Delete -Y) \t")

def file_conversion(input_file, output_file_pattern, chunksize):

    with open(input_file,"r+") as fin:
        # ignore headers of input files
        for i in range(1):
            fin.__next__()
        reader = csv.reader(fin, delimiter=',')
        for i, chunk in enumerate(chunked(reader, chunksize)):
            with open(output_file_pattern.format(i), 'w', newline='') as fout:
                writer = csv.writer(fout,reader,delimiter='^')
                writer.writerow(headers)
                writer.writerows(chunk)
                print("Successfully converted into", output_file_pattern)

The program works now I want to add act,cc and cn in every line of output delimiter files. The lines of output files should start like the line below

act^cc^cn^restofcsvdelimitierfiles. 
Paul Brennan
  • 2,638
  • 4
  • 19
  • 26
Atom Store
  • 961
  • 1
  • 11
  • 35
  • 1
    chunked is not defined in this bit of code, but you should iterate through chunks into rows , then prepend the act^cc^cn^ to the strings. Then collect the rows back to a new chunk, and write that. – Paul Brennan Dec 17 '20 at 18:47
  • chunked is used so that if the no of lines is more than 10000(in this case), it will create a new output file. The number of lines in the output file is limited to 10000. If possible, can you give the example? – Atom Store Dec 18 '20 at 03:34
  • I want to see the output of chunked so that I can tell if it is iterable. I don't know where you got the function from as this code has no includes... – Paul Brennan Dec 18 '20 at 03:41
  • 1
    I have done this.First I created a new file where act, cc, and cn are appended, and then I converted it into the delimited files. – Atom Store Dec 18 '20 at 05:40

1 Answers1

0

This solved my problem

def add_column_in_csv(input_file, output_file, transform_row,transform_row1,transform_row2):
                with open(input_file, 'r') as read_obj:
                    with open(output_file, 'w', newline='') as write_obj:
                        reader = csv.reader(read_obj,delimiter=',')
                        writer = csv.writer(write_obj)
                        for row in reader:
                            transform_row(row,reader.line_num)
                            transform_row1(row,reader.line_num)
                            transform_row2(row,reader.line_num)
                            writer.writerow(row)
add_column_in_csv(path0,path1, lambda row, line_num: row.insert(0, act),lambda row, line_num: row.insert(1,cc),lambda row, line_num: row.insert(2,cn))
Atom Store
  • 961
  • 1
  • 11
  • 35